How to finish all activity when exit from child activity

微笑、不失礼 提交于 2019-12-08 06:23:55

问题


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);


来源:https://stackoverflow.com/questions/11814851/how-to-finish-all-activity-when-exit-from-child-activity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!