coreSOLO
coreSOLO

Reputation: 3619

How to return result 'in a natural way' when the called the called Activity exits?

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.

Upvotes: 10

Views: 7729

Answers (3)

Alexey
Alexey

Reputation: 7442

The best way I found is to override finish() method:

@Override
public void finish() {
    Intent data = new Intent();
    data.putExtra(INTENT_PARAM_OPTIONS, options);
    setResult(RESULT_OK, data);
    super.finish();
}

In that case, your activity will always return data, even if the user presses back button (or activity bar up button).

And I think its perfectly natural to do this in some circumstances. If you have an activity that is made to edit one big entity (shipping order for instance), and a smaller activity inside that activity that modifies 1 small part of big entity (shipping address), you might aswell always save any changes inside smaller activity, because user can cancel all the changes by cancelling the big activity.

Upvotes: 9

Kiril Kirilov
Kiril Kirilov

Reputation: 11257

You can try to override onBackPressed or onKeyDown, and setResult(RESULT_OK, intent) there.

Upvotes: 1

CommonsWare
CommonsWare

Reputation: 1007359

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.

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 NextAct were a ListActivity, a typical pattern is for clicking on a list item to be considered "OK" (i.e., call setResult() and finish() from onListItemClick()), and BACK to mean "I didn't really mean to start NextAct, sorry".

I need to know why this is happening

You are calling setResult() too late. If the user presses BACK, by the time of onPause(), the result (RESULT_CANCELED) has already been determined.

if onResume is not a proper place to put setResult logic

onResume() would not be a proper place to "put setResult logic"

what is the most natural way to do it?

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 call setResult() there before chaining to the superclass with super.onBackPressed().

Upvotes: 13

Related Questions