Finish all activities but keep the first

1.4k views Asked by At

The app has this flows:

1) Home -> Activity A -> Activity B -> Activity C -> Activity A -> Activity B -> Activity C -> etc.

2) Home -> Activity C -> Activity B -> Activity C -> Activity A -> Activity B -> Activity C -> etc.

3) Home -> Activity D -> Activity B -> Activity C -> Activity A -> Activity B -> Activity C -> etc.

Activity B has a button that must close all activities except Home and the first Activity. What is the best way to do it?

If I give same taskAffinity to A, B and C and use finishAffinity() then all activities will be closed.

2

There are 2 answers

1
Ankita On BEST ANSWER

Try this i.e, if you want to close all activities except MainActivity and open otherActivity from this.

Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |  Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);
0
Sagar Vasoya On

Just use finishAffinity() after start simple activity

Activity.finishAffinity() vs Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK

https://stackoverflow.com/a/33517795/5069323