How to clear the Android Stack of activities?(如何清除 Android Stack 的活动?)
问题描述
我在 Android 中有一个包含多个活动的应用程序,我希望用户能够通过按菜单按钮退出.我的问题是
I have an application with several Activities in Android and I want the user to be able to log-out by pressing a menu button. The problem I have is that
A) Android 不允许您终止应用程序并且
B) 即使当我再次将用户发送到 LoginActivity 时,他们也可以随时按 back 并立即回到他们所在的上一个活动.
A) Android doesn't let you terminate the application and
B) even when I send the user to the LoginActivity again they can always press back and get right back to the previous activity they were in.
我已经尝试使用以下两个标志启动 Activity:
I already tried to launch the Activity with the two following flags:
Intent intent = new Intent(this, LoginActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
我也自己尝试过.
我还尝试在 startActivity(intent) 之后调用 finish(),因为我在另一个 StackOverflow 中阅读问题.
I also tried calling finish() after startActivity(intent) as I read in another StackOverflow
question.
推荐答案
在您的登录活动中,覆盖后退按钮,因此它会隐藏您的应用而不是完成活动:
In your login activity, override the back button, so it hides your app instead of finishing the activity:
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
moveTaskToBack(true);
return true;
}
return super.onKeyDown(keyCode, event);
}
还要确保在根活动上设置 android:alwaysRetainTaskState="true",这样 Android 不会在用户不活动 30 分钟后清除您的堆栈(包括登录活动).
Also be sure to set android:alwaysRetainTaskState="true" on the root activity, so Android doesn't clear your stack (including the login activity) after 30min of inactivity from user.
然后在登录成功时调用finish().
Then just call finish() when there is a successful login.
这篇关于如何清除 Android Stack 的活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何清除 Android Stack 的活动?
基础教程推荐
- NSString intValue 不能用于检索电话号码 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
