Program: 142132
Project: 142132
Buggy source:
/** 
 * Process user returning home from the form entry activity. Triggers form submission cycle, cleans up some session state.
 * @param resultCode exit code of form entry activity
 * @param intent     The intent of the returning activity, with thesaved form provided as the intent URI data. Null if the form didn't exit cleanly
 * @return Flag signifying that caller should fetch the next activity inthe session to launch. If false then caller should exit or spawn home activity.
 */
private boolean processReturnFromFormEntry(int resultCode,Intent intent){
  AndroidSessionWrapper currentState=CommCareApplication._().getCurrentSessionWrapper();
  FormRecord current=currentState.getFormRecord();
  if (current == null) {
    Toast.makeText(this,"String_Node_Str",Toast.LENGTH_LONG).show();
    Logger.log(AndroidLogger.TYPE_ERROR_WORKFLOW,"String_Node_Str");
    clearSessionAndExit(currentState,true);
    return false;
  }
  if ((intent != null && intent.getBooleanExtra(FormEntryActivity.IS_ARCHIVED_FORM,false)) || FormRecord.STATUS_COMPLETE.equals(current.getStatus()) || FormRecord.STATUS_SAVED.equals(current.getStatus())) {
    currentState.reset();
    if (wasExternal) {
      this.finish();
    }
 else {
      goToFormArchive(false,current);
    }
    return false;
  }
  if (resultCode == RESULT_OK) {
    Uri resultInstanceURI=null;
    if (intent != null) {
      resultInstanceURI=intent.getData();
    }
    if (resultInstanceURI == null) {
      CommCareApplication._().reportNotificationMessage(NotificationMessageFactory.message(StockMessages.FormEntry_Unretrievable));
      Toast.makeText(this,"String_Node_Str",Toast.LENGTH_LONG).show();
      Logger.log(AndroidLogger.TYPE_ERROR_WORKFLOW,"String_Node_Str");
      clearSessionAndExit(currentState,true);
      return false;
    }
    Cursor c=null;
    String instanceStatus;
    try {
      c=getContentResolver().query(resultInstanceURI,null,null,null,null);
      if (!c.moveToFirst()) {
        throw new IllegalArgumentException("String_Node_Str");
      }
      instanceStatus=c.getString(c.getColumnIndexOrThrow(InstanceProviderAPI.InstanceColumns.STATUS));
    }
  finally {
      if (c != null) {
        c.close();
      }
    }
    boolean complete=InstanceProviderAPI.STATUS_COMPLETE.equals(instanceStatus);
    if (complete) {
      checkAndStartUnsentFormsTask(false,false);
      uiController.refreshView();
      if (wasExternal) {
        this.finish();
        return false;
      }
      boolean terminateSuccessful;
      try {
        terminateSuccessful=currentState.terminateSession();
      }
 catch (      XPathTypeMismatchException e) {
        UserfacingErrorHandling.logErrorAndShowDialog(this,e,true);
        return false;
      }
      if (!terminateSuccessful) {
        return false;
      }
    }
 else {
      clearSessionAndExit(currentState,false);
      return false;
    }
  }
 else   if (resultCode == RESULT_CANCELED) {
    Logger.log(AndroidLogger.TYPE_FORM_ENTRY,"String_Node_Str");
    if (current.getStatus().equals(FormRecord.STATUS_UNSTARTED)) {
      FormRecordCleanupTask.wipeRecord(this,currentState);
    }
    if (wasExternal) {
      currentState.reset();
      this.finish();
      return false;
    }
 else     if (current.getStatus().equals(FormRecord.STATUS_INCOMPLETE)) {
      currentState.reset();
      goToFormArchive(true,current);
      return false;
    }
 else {
      currentState.getSession().stepBack();
      currentState.setFormRecordId(-1);
    }
  }
  return true;
}

Fixed version (ground truth):
/** 
 * Process user returning home from the form entry activity. Triggers form submission cycle, cleans up some session state.
 * @param resultCode exit code of form entry activity
 * @param intent     The intent of the returning activity, with thesaved form provided as the intent URI data. Null if the form didn't exit cleanly
 * @return Flag signifying that caller should fetch the next activity inthe session to launch. If false then caller should exit or spawn home activity.
 */
private boolean processReturnFromFormEntry(int resultCode,Intent intent){
  AndroidSessionWrapper currentState=CommCareApplication._().getCurrentSessionWrapper();
  FormRecord current=currentState.getFormRecord();
  if (current == null) {
    Toast.makeText(this,"String_Node_Str",Toast.LENGTH_LONG).show();
    Logger.log(AndroidLogger.TYPE_ERROR_WORKFLOW,"String_Node_Str");
    clearSessionAndExit(currentState,true);
    return false;
  }
  if ((intent != null && intent.getBooleanExtra(FormEntryActivity.IS_ARCHIVED_FORM,false)) || FormRecord.STATUS_COMPLETE.equals(current.getStatus()) || FormRecord.STATUS_SAVED.equals(current.getStatus())) {
    currentState.reset();
    if (wasExternal) {
      setResult(RESULT_CANCELED);
      this.finish();
    }
 else {
      goToFormArchive(false,current);
    }
    return false;
  }
  if (resultCode == RESULT_OK) {
    Uri resultInstanceURI=null;
    if (intent != null) {
      resultInstanceURI=intent.getData();
    }
    if (resultInstanceURI == null) {
      CommCareApplication._().reportNotificationMessage(NotificationMessageFactory.message(StockMessages.FormEntry_Unretrievable));
      Toast.makeText(this,"String_Node_Str",Toast.LENGTH_LONG).show();
      Logger.log(AndroidLogger.TYPE_ERROR_WORKFLOW,"String_Node_Str");
      clearSessionAndExit(currentState,true);
      return false;
    }
    Cursor c=null;
    String instanceStatus;
    try {
      c=getContentResolver().query(resultInstanceURI,null,null,null,null);
      if (!c.moveToFirst()) {
        throw new IllegalArgumentException("String_Node_Str");
      }
      instanceStatus=c.getString(c.getColumnIndexOrThrow(InstanceProviderAPI.InstanceColumns.STATUS));
    }
  finally {
      if (c != null) {
        c.close();
      }
    }
    boolean complete=InstanceProviderAPI.STATUS_COMPLETE.equals(instanceStatus);
    if (complete) {
      checkAndStartUnsentFormsTask(false,false);
      uiController.refreshView();
      if (wasExternal) {
        setResult(RESULT_CANCELED);
        this.finish();
        return false;
      }
      boolean terminateSuccessful;
      try {
        terminateSuccessful=currentState.terminateSession();
      }
 catch (      XPathTypeMismatchException e) {
        UserfacingErrorHandling.logErrorAndShowDialog(this,e,true);
        return false;
      }
      if (!terminateSuccessful) {
        return false;
      }
    }
 else {
      clearSessionAndExit(currentState,false);
      return false;
    }
  }
 else   if (resultCode == RESULT_CANCELED) {
    Logger.log(AndroidLogger.TYPE_FORM_ENTRY,"String_Node_Str");
    if (current.getStatus().equals(FormRecord.STATUS_UNSTARTED)) {
      FormRecordCleanupTask.wipeRecord(this,currentState);
    }
    if (wasExternal) {
      currentState.reset();
      setResult(RESULT_CANCELED);
      this.finish();
      return false;
    }
 else     if (current.getStatus().equals(FormRecord.STATUS_INCOMPLETE)) {
      currentState.reset();
      goToFormArchive(true,current);
      return false;
    }
 else {
      currentState.getSession().stepBack();
      currentState.setFormRecordId(-1);
    }
  }
  return true;
}