Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

android

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.

like image 259
coreSOLO Avatar asked Feb 20 '11 23:02

coreSOLO


People also ask

How can I get my activity results back?

We just call the intent with startActivityForResult , indicating we want a result back. We set up the onActivityResult() callback handler to receive the results. Finally, we make sure that the second activity returns a result with setResult() before closing the activity.

What is used to send back data to the activity?

We can send the data using the putExtra() method from one activity and get the data from the second activity using the getStringExtra() method.

When moving from one activity to another activity which method is used to return data back to its parent?

How pass data from second activity to first activity when pressed back Android? When moving from one activity to another activity which method is used to return data back to its parent? The setResult method takes an int result value and an Intent that is passed back to the calling Activity.

How pass data from second activity to first activity when pressed back Android?

Start Activity2 with startActivityForResult and use setResult method for sending data back from Activity2 to Activity1. In Activity1 you will need to override onActivityResult for updating TextView with EditText data from Activity2. If you can, also use SharedPreferences for sharing data between Activities.


2 Answers

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().

like image 61
CommonsWare Avatar answered Sep 22 '22 10:09

CommonsWare


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.

like image 27
Alexey Avatar answered Sep 23 '22 10:09

Alexey