如何清除活动的Android软件开发包? [英] How to clear the Android Stack of activities?
问题描述
我有几个活动的Android应用程序,我希望用户能够登录,由pressing菜单按钮。我的问题是,
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
他们又总能preSS的回的和马上回了previous活动他们是在
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.
我已经尝试启动活动有以下两个标志:
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);
我也试过跟他们每个人自己。
I also tried with each one of them by themselves.
我也打过电话完成()
在 startActivity(意向)
因为我读的另一计算器
的问题。
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 =根活动真,因此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软件开发包?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!