What Is Working Fine
I have 2 activities in my app. First activity calls second actifity for results.
Second activity shows a new layout and lets user perform certain actions. There is a “OK” button. When user presses this button, second activity is finished and user goes back to first activity.
Under the hood, first activity calls second ativity like this:
Intent intent = new Intent(this, NextAct.class);
intent.putExtra("input", input);
this.startActivityForResult(intent, 99);
On “OK” button press, second activity returns with result like this:
Intent intent = new Intent();
intent.putExtra("output", output);
setResult(RESULT_OK, intent);
finish();
After that, first activity’s onActivityResult is called with results successfully:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// requestCode is 99
// resultCode is -1
// data holds my output
}
Above setup is working perfectly
What Is Not Working
Now I had a simple requirement, I wanted the user to close the second activity not with an “OK” button, but in a natural way with “HARDWARE BACK” button.
I tried moving the setResult logic in onStop and onDestroy methods of second activity, but it turned out that onActivityResult of first activity is called before onStop or onDestroy methods of second activity and as a result setResult logic does not get a chance to run at all.
Then I tried moving the setResult logic in onPause method of second activity like this
protected void onPause() {
super.onPause();
Intent intent = new Intent();
intent.putExtra("output", output);
setResult(RESULT_OK, intent);
//finish(); enabling or disabling this does not work
}
But although onPause is being called well before onActivityResult and setResult logic runs properly, still I get all null values in onActivityResult
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// requestCode is 99
// resultCode is 0
// data comes as null
}
I need to know why this is happening and if onResume is not a proper place to put setResult logic, what is the most natural way to do it?
Thanks much.
That is not usually “natural” for a
startActivityForResult()scenario. The BACK button should allow the user to tell you “No, it is not OK” (i.e., cancel).This does not mean you necessarily have to have an OK button. For example, if
NextActwere aListActivity, a typical pattern is for clicking on a list item to be considered “OK” (i.e., callsetResult()andfinish()fromonListItemClick()), and BACK to mean “I didn’t really mean to startNextAct, sorry”.You are calling
setResult()too late. If the user presses BACK, by the time ofonPause(), the result (RESULT_CANCELED) has already been determined.onResume()would not be a proper place to “put setResult logic”Possibly what you have with the OK button is the “most natural way to do it”.
However, if for some strange reason you really do want the BACK button to mean “OK”, override
onBackPressed()and callsetResult()there before chaining to the superclass withsuper.onBackPressed().