1

I have a generic tutorial-based application that passes user information to a second activity. On the second activity, the user opts whether to "agree" via a CheckBox. When the user clicks the submit button, they are suppose to return to the previous activity, with a SnackBar confirming whether they checked the agree CheckBox.

The problem is, the submit button on the second activity does not appear to function correctly. It uses the finishActivity(int) method, but nothing happens.


What greatly confuses me is that if I then select the back button on my phone, I am returned to my first activity. On top of that, it appears to still pass the information set with setResult(), and not only correctly returns the expected requestCode, but also correctly returns the intent and determines whether the CheckBox was set to enabled==true or not.

This does not happen, if I do not click the submit button, first. If I press the back button on the second activity, without first pressing the submit button, the application closes.


The submit button on my MainActivity initiates the MoveToConfirmationActivity method:

/** Moves us to the second activity, the Confirmation Activity */
public void MoveToConfirmationActivity(View view) {

    Intent intent = new Intent(this, ConfirmActivity.class);

    intent.putExtra(REQUEST_CONFIRMATION, REQUEST_CONFIRMATION_INT);

    startActivityForResult(intent, REQUEST_CONFIRMATION_INT);
}

I have also overridden the onActivityResult(int, int, Intent) as follows:

/** Runs when this activity is returned to by another activity */
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if(requestCode == REQUEST_CONFIRMATION_INT) {
        // Retrieve the accepted flag from the Confirm Activity, with the default as false.
        boolean accepted = data.getBooleanExtra(ConfirmActivity.AGREE, false);

        if(accepted) {
            SnackbarToast(getString(R.string.agree));
        }
        else {
            SnackbarToast(getString(R.string.disagree));
        }
    }
}

For point of clarity, I do not have requestCode set anywhere outside of onActivityResult() for it to misinterpret its own value.


The submit button on my ConfirmActivity initiates the StartReturnToMainActivity method:

/** Creates an intent to pass the checkbox value, and returns us to MainActivity. */
public void StartReturnToMainActivity(View view) {

    Intent intent = new Intent(this, MainActivity.class);
    intent.putExtra(AGREE, mAgreeCheckbox.isChecked());

    setResult(RESULT_OK, intent);
    finishActivity(mMainRequestInt);
}

In hindsight, I realise that it has always been doing this. While the submit button has never worked, I have always been able to use Back to go back to the previous activity, after selecting submit. It was only after including setResult(RESULT_OK, intent); that my first activity has been reacting to the desired selection. The answer from an alternate question suggested that this was a requirement.

I can confirm that setResult and finishActivity still apply the logic of passing the resultCode, requestCode and intent back into the first activity. I have worked out that any 'changes' I make after selecting submit will not move across with the back button, unless I select submit again.


I have gone through the tutorials on the Android Developers website, and they have helped considerably in leading up to this point; however, in respect to the code on the side of ConfirmActivity, all I can find is the implication that I should already know what I am doing. I believe the main tutorial I followed intended the second activity to come from an external source.

I found a lot of useful information, here on StackExchange; but across the many similar questions, the final answer seems to be that finishActivity(int) should ultimately always send me back to the previous activity.

Thanks to another question, I have worked out that I can "code in" the press of the back button immediately after my finishActivity() with onBackPressed(). With this consideration, everything appears to work as intended. However, I consider this a less-than-desirable hack. I would much rather have an understanding of why it is not working as expected, and how I should properly return to the previous activity.

Is this the correct implementation of finishActivity(int)? Why do I still need to hit the Back button on my phone for it to work, and how do I fix this?

Community
  • 1
  • 1
Gnemlock
  • 325
  • 6
  • 22
  • have you tried just (finish();) – Tasos Aug 17 '16 at 14:00
  • @Tasos, I have. `finish()` outright closes the app – Gnemlock Aug 17 '16 at 14:02
  • Strange -- That will happen if you are on the Main-activity. (finish();) just finishes the current activity so the previous Activity appears again. – Tasos Aug 17 '16 at 14:05
  • @Tasos, I am also using it on my main activity on an exit button. It still closes from the main activity, including if I've returned there from the second activity. – Gnemlock Aug 17 '16 at 14:08
  • There is no way (ACTIVITY B with finish();) would Kill the (MAIN ACTIVITY) – Tasos Aug 17 '16 at 14:15
  • Are you sure its an Activity where ( the user opts whether to "agree" via a CheckBox) and not a DIALOG box?? – Tasos Aug 17 '16 at 14:18
  • @Tasos, interestingly enough, it does not appear to *kill* it. I have logs on all of my lifecycle overrides. While the intentional "kill" button runs `onPause`, `onStop`, `onDestroy`; implementing it in the second activity only appears to run its `onPause` method. – Gnemlock Aug 17 '16 at 14:24
  • It does not appear to be a dialog box.. I was told to use `class ConfirmActivity extends ActionBarActivity implements View.OnClickListener` – Gnemlock Aug 17 '16 at 14:25
  • @Tasos, well that sucks. It appears `finish()` *did* solve this problem. After I turned off my computer for a day, and did another refresh of Android Studio :/ – Gnemlock Aug 22 '16 at 05:58
  • If you care to provide a short answer pointing out *why* we use *finish* and not *finishActivity*, I'll be sure to mark your answer as correct, seeing a you technically gave me the right answer from the very get go. – Gnemlock Aug 22 '16 at 06:05
  • dont worry its ok, glad to help :) – Tasos Aug 22 '16 at 06:55

1 Answers1

0

As suggested by Tasos, using finish() instead of finishActivityint() did actually fix my problem. To an extent..

It seems this solution only made a difference after I shut down my computer, turned it back on, and performed a rebuild of my project.

I believe this might be somewhat new, given that it contradicts the highly-voted answer I previously found, here on StackOverflow. That said, if anyone else has a similar "silly problem" like this;

  1. Try turning it on and off again
  2. Do a rebuild of the project

I have previously had weird issues that fixed themselves upon rebuilding; but never an issue that solved itself only after a complete system restart.

Gnemlock
  • 325
  • 6
  • 22