Java – Android – Kills multiple previous activities when starting a new activity

Android – Kills multiple previous activities when starting a new activity… here is a solution to the problem.

Android – Kills multiple previous activities when starting a new activity

So I know that I can terminate the current activity and start a new one by simply doing the following:

//Presumably called from class A
Intent i = new Intent(A.this, B.class);
startActivity(i);
A.this.finish();

I

realized there was some overly verbose code in it, and I just wanted to be clear. So if we assume that this is called from some activity A, then we know that A will end, B will start, and when you press the back button in B, you will be taken to the activity running before Activity A.

My question is, can this go further? For example, how will I achieve the following goals?

//From class A
Intent i = new Intent(A.this, B.class);
startActivity(i);

from class B
Intent i = new Intent(B.this, C.class);
startActivity(i);
A.this.finish(); something equivalent
B.this.finish();

Thanks in advance for any insight! 🙂

Solution

Why I believe you’re looking for the intent flag FLAG_ACTIVITY_CLEAR_TOP .

This clears the background stack of all activities (or all activities if no new activity has ever been called) by the last instance of the target activity class.

Related Problems and Solutions