1

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)

Sandip Patel
  • 501
  • 4
  • 14

1 Answers1

2

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.

Karakuri
  • 38,365
  • 12
  • 84
  • 104
  • Another options is to use EventBus (Otto http://square.github.io/otto/ or EventBus https://github.com/greenrobot/EventBus ), this more more of a replacement for option #3. – bond Jul 23 '15 at 14:59