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.