-1

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.

Ivan Fork
  • 812
  • 1
  • 9
  • 22

2 Answers2

3

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);
Ankita
  • 1,129
  • 1
  • 8
  • 15
1

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
Sagar Vasoya
  • 158
  • 1
  • 12