Dealing with phone's back button: Back button on home activity always to cause app exit

偶尔善良 提交于 2020-01-16 18:25:13

问题


Let's say I have 4 activities in my app:
A (main activity)
B
C
D

I would like that pressing back in Activity A always produce application exit. In my case if activity flow goes like this A > B > C > A, then if I push back button, I will go to activity C. I want, at that moment, my app to exit.

I assume, app should somehow delete activity history when main activity is active.
How is this to be done?

Thanks


回答1:


When you launch your home activity do so with the clear top flag set. This causes the back stack to be cleared.

Intent intent = new Intent(this, HomeActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);



回答2:


If you want the above behaviour then use the FLAG_ACTIVITY_CLEAR_TOP to launch A. This will clear the all the activities above A



来源:https://stackoverflow.com/questions/6046234/dealing-with-phones-back-button-back-button-on-home-activity-always-to-cause-a

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