Sandip Patel
Sandip Patel

Reputation: 501

How to manage multiple activity interactions in Android?

In my activity, i'm calling activity B from activity A (in back stack A->B), than again activity C from B (in back stack A-> B-> C) for some result to reflect on activity A, than how to jump to activity A on button click with carrying some result from activity C to reflect back in activity A(while clearing back stack of activity B,C)

Upvotes: 1

Views: 184

Answers (1)

Karakuri
Karakuri

Reputation: 38605

Option 1: You can chain calls of startActivityForResult() and "unwind" the chain in the onActivityResult() callbacks.

  • Activity A calls startActivityForResult() to start Activity B
  • Activity B calls startActivityForResult() to start Activity C
  • Call setResult(RESULT_OK) and then finish() in Activity C
  • Result of Activity C is received in onActivityResult() of Activity B. Process the result (if needed).
  • Call setResult(RESULT_OK) and then finish() in Activity B
  • Result of Activity B is received in onActivityResult() of Activity A. Process the result.

Option 2: Start Activity A again, but add flags to the intent.

Intent intent = new Intent(this, ActivityA.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
// add more data to Intent
startActivity(intent);
finish();

This intent will be received in the onNewIntent() method of Activity A.

EDIT

Option 3: Use LocalBroadcastManager to send broadcasts locally (within your process only). This requires registering a BroadcastReceiver dynamically in Activity A, then sending the broadcast from Activity C. You will still need to use one of the two techniques above to get back to Activity A and clear the stack; this just changes how the data is transmitted back to Activity A.

Upvotes: 2

Related Questions