I have a application which have, let say, Activities A, B & C. B can be started by A as well as any other application's activity also. But C can only be started by B. On pressing back on C, user navigates to B(that is ok) but i have a button on C, which on pressing user should navigate to A or any other application's activity which launched B.
Asked
Active
Viewed 59 times
0
-
It may helps you: http://stackoverflow.com/a/27795433/3922207 – Anggrayudi H Feb 09 '15 at 10:00
-
You want to be able to navigate from `C` back to the Activity that started `B`? – David Wasser Feb 09 '15 at 10:57
-
@AlbertNicko: solution mentioned in the link says to clear back history & hence clearing the activity A which launched B. But i want to go back to A on button click. – Learner Feb 09 '15 at 13:04
-
@DavidWasser: yes.. i want to navigate back to activity that started B, with stack clearing entries of B & C. – Learner Feb 09 '15 at 13:06
1 Answers
0
To clear B
off the stack and return to the previous Activity, do the following:
in C
, launch B
with an EXTRA:
Intent intent = new Intent(this, B.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.putExtra("finish", true);
startActivity(intent);
Using CLEAR_TOP and SINGLE_TOP here ensure that this will go back to the existing instance of B
, and not create a new one.
In B
, override onNewIntent()
as follows:
@Override
protected void onNewIntent(Intent intent) {
if (intent.hasExtra("finished")) {
// Wants to finish this Activity to return to the previous one
finish();
}
}
NOTE: This only works if B
is still in the Activity stack when it launches C
(ie: it doesn't call finish()
when launching C
).
An alternative would be to have B
launch C
using startActivityForResult()
. In this case, C
could return information (either in the resultCode
or the returned Intent
) that would allow B
to determine if it should or should not finish.

David Wasser
- 93,459
- 16
- 209
- 274