简体   繁体   English

如何清除按钮上的活动堆栈在Android中单击

[英]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. 我有一个问题,我在我的应用程序中有一个注销按钮,我们在其上调用了应用程序登录屏幕,但此时当用户按下Android手机的后退按钮时,他再次进入应用程序而没有身份验证,这是不可取的。 I want when we click on Logout button All previous Activity Stack being cleared or we can say that All previous onPause Activities have to be cleared. 我希望当我们点击Logout按钮时,所有以前的活动堆栈都被清除,或者我们可以说所有以前的onPause活动都必须被清除。

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

Alternative solution is to end your current activity by calling finish(); 替代解决方案是通过调用finish();来结束当前活动finish(); after you start the login activity 在您开始登录活动之后

// logout button handler
startActivity(new Intent(context, LoginActivity.class));
finish();

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 . 你需要使用标志FLAG_ACTIVITY_NEW_TASK

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM