Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return a result through multiple activities

in some part of my application there is a structure of activities like this:

enter image description here

Activity A is my home activity where I come back after each process or flow.
Activity B is a simple confirmation activity.
Activity C is another confirmation activity.
Activity D does some process and finally it gets back the result to my home activity (Activity A).

Requirements:

Activity B and C:

  • must be there and cannot be merged in one.
  • when it is clicked on the cancel button it calls finish() and does nothing.
  • when it is clicked on the OK button it follows the flow and the current activity calls finish()

Question:

I was thinking about using startActivityForResult() but I have never used it to pass a result through multiple activities..
I also was thinking to pass a handler (created in Activity A) which calls some method on Activity A and is executed on Activity D...

How could I implement it?

like image 968
jalv1039 Avatar asked Feb 03 '12 21:02

jalv1039


People also ask

How do you finish multiple activities?

When the user is on Activity D and click a button called exit, the application should go back to Activity B and finish the Activities C and D.

How we can capture activity result event after returning from an activity start for result?

The android startActivityForResult method, requires a result from the second activity (activity to be invoked). In such case, we need to override the onActivityResult method that is invoked automatically when second activity returns result.

When on activity result is called?

android - onActivityResult gets called when Activity starts, not when its finished - Stack Overflow. Stack Overflow for Teams – Start collaborating and sharing organizational knowledge.


2 Answers

You might like to make use of the intent flag FLAG_ACTIVITY_FORWARD_RESULT as described in Intent when starting activities B and C

public static final int FLAG_ACTIVITY_FORWARD_RESULT

Since: API Level 1

If set and this intent is being used to launch a new activity from an existing one, then the reply target of the existing activity will be transfered to the new activity. This way the new activity can call setResult(int) and have that result sent back to the reply target of the original activity.

That way A should pick up any data sent back in the extras sent back from D

like image 119
NickT Avatar answered Sep 19 '22 18:09

NickT


Yup, great formatting. And you can -- and probably should -- definitely call startActivityForResult() from each of Activity A, B, and C (and don't finish() right away). In B and C you can check for a successful result and finish(), passing the result on back to A.

    @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) {     if((resultCode == RESULT_OK) && (requestCode == MY_RESULT_CODE)) {         setResult(RESULT_OK, data);         finish();     } } 

If you want B and C to disappear regardless, do the following instead:

    @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) {     setResult(resultCode, data);     finish(); } 
like image 23
Brian Dupuis Avatar answered Sep 23 '22 18:09

Brian Dupuis