How to clear Activity Stack on Button Click in Android
I have a question that I have a logout button in my App on which we have called an App login Screen but at this point when user press the Back Button of Android Phone, he entered in the App again without Authentication, which is not desirable. I want when we click on Logout button All previous Activity Stack being cleared or we can say t开发者_运维知识库hat All previous onPause Activities have to be cleared.
Please Suggest me the right solution for this problem.
Thanks in advance.
As far as I understood the login screen would be the first screen after the splash one so if login screen is in stack you can call again login screen like the below to achieve this
Intent launch = new Intent(context, LoginActivity.class);
launch.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(launch);
After logout start login activity like this:
Intent launch = new Intent(context, LoginActivity.class);
launch.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(launch);
you need to use flag FLAG_ACTIVITY_NEW_TASK
.
Alternative solution is to end your current activity by callingfinish();
after you start the login activity
// logout button handler
startActivity(new Intent(context, LoginActivity.class));
finish();
精彩评论