Reputation: 501
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
Reputation: 38605
Option 1: You can chain calls of startActivityForResult()
and "unwind" the chain in the onActivityResult()
callbacks.
startActivityForResult()
to start Activity BstartActivityForResult()
to start Activity CsetResult(RESULT_OK)
and then finish()
in Activity ConActivityResult()
of Activity B. Process the result (if needed).setResult(RESULT_OK)
and then finish()
in Activity BonActivityResult()
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