Reputation: 822
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.
Upvotes: -1
Views: 1486
Reputation: 176
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
Upvotes: 1
Reputation: 1159
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);
Upvotes: 3