How to finish all activity when exit from child ac

2019-06-05 02:42发布

问题:

Example : I have 3 Activities, A,B, and C. from Activity A I open Activity B then From B open Activity C. Then I exit application by code :

Intent intent = new Intent(Intent.ACTION_MAIN); 
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();
System.exit(0);

I use this code for exit app. But when restart app, back again to recent Activity. My question, How to finish all Activities when exit from app?

回答1:

You must be back to your main activity first by this code:

Intent home = new Intent(this, mainActivity.class);
home.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(home);

And then you will exit your application from mainActivity like this:

finish();
System.exit(0);

Hope it helps.



回答2:

Whenever you are calling from one Activity to another activity, try to clear the activites stack by using the following flag:

intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

Hope it helps.



回答3:

Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);//***Change Here***
startActivity(intent);
finish();
System.exit(0);