I am trying to setResult after the BACK button was pressed. I call in onDestroy
Intent data = new Intent();
setResult(RESULT_OK, data)
But
If you want to set some custom RESULT_CODE in onBackPressed event then you need to first set the result and then call the super.onBackPressed() and you will receive the same RESULT_CODE in the caller activity's onActivityResult method
@Override
public void onBackPressed()
{
setResult(SOME_INTEGER);
super.onBackPressed();
}
You should override onOptionsItemSelected like this:
@Override
public boolean onOptionsItemSelected(final MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
final Intent mIntent = new Intent();
mIntent.putExtra("param", "value");
setResult(RESULT_OK, mIntent);
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
I refactored my code. Initially I prepared some data and set it as activity result in onDestroy (this did not work). Now I set activity data each time the data to be returned is updated, and have nothing in onDestroy.
Refer onActivityResult(int, int, Intent) doc
Solution is to check the resultCode for value Activity.RESULT_CENCELED. If yes, then it means that either BACK was pressed or the activity crashed. Hope it works for you guys, works for me :).