Clear activity stack in android

旧城冷巷雨未停 提交于 2020-01-12 11:51:11

问题


I have a LoginActivity as my launcher screen. And SettingsActivity as my fifth screen.

I have a logout button in settings screen. On clcik of this button, how can i go to Screen-1 (i.e LoginActivity) by clearing all remaining activities (i.e 2nd, 3rd, 4th) from activity stack?

Note : i have finished LoginActivity but not remaining activities. Thanks in advance


回答1:


Simple, set an onclick on your logout button and then add this to your intent:

Intent newIntent = new Intent(this, login.class);

    newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    startActivity(newIntent);
    finish();

This should clear your entire stack above login.class

And if you're feeling real fun today, add

    newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

Which will get rid of that annoying "show the android homescreen" issue on the S3



来源:https://stackoverflow.com/questions/12621809/clear-activity-stack-in-android

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