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?