Say I have 3 activities A, B and C. A leads to B which leads to C. I would like to be able to move back and forth between A and B but I want to finish both A and B once C gets started. I understand how to close B when starting C via the intent but how do I also close A when C gets started?
Asked
Active
Viewed 251 times
2 Answers
1
Use this flag when you are opening the C acitivity.
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
This will clear all the activities on top of C.

Ajay S
- 48,003
- 27
- 91
- 111
-
1Sounds to me like A and B are **under** C (ie: before C), not over C. In which case, `FLAG_ACTIVITY_CLEAR_TOP` won't help. – David Wasser Mar 21 '13 at 20:51
0
Since A
is your root (starting) activity, consider using A
as a dispatcher. When you want to launch C
and finish all other activities before (under) it, do this:
// Launch ActivityA (our dispatcher)
Intent intent = new Intent(this, ActivityA.class);
// Setting CLEAR_TOP ensures that all other activities on top of ActivityA will be finished
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
// Add an extra telling ActivityA that it should launch ActivityC
intent.putExtra("startActivityC", true);
startActivity(intent);
in ActivityA.onCreate()
do this:
super.onCreate();
Intent intent = getIntent();
if (intent.hasExtra("startActivityC")) {
// Need to start ActivityC from here
startActivity(new Intent(this, ActivityC.class));
// Finish this activity so C is the only one in the task
finish();
// Return so no further code gets executed in onCreate()
return;
}
The idea here is that you launch ActivityA (your dispatcher) using FLAG_ACTIVITY_CLEAR_TOP
so that it is the only activity in the task and you tell it what activity you want it to launch. It will then launch that activity and finish itself. This will leave you with only ActivityC in the stack.

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