安卓:从后面堆栈中删除所有的previous活动 [英] Android: Remove all the previous activities from the back stack
问题描述
当我点击的注销我个人资料活动按钮,我想利用用户的登录页,在那里他需要使用新的凭据。
When i am clicking on Logout button in my Profile Activity i want to take user to Login page, where he needs to use new credentials.
所以我用这个code:
Hence i used this code:
Intent intent = new Intent(ProfileActivity.this,
LoginActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
在 onButtonClick
注销按钮。
但问题是,当我点击设备的后退按钮就可以把我的ProfileActivity登录活动。我期待应用程序应该关闭的时候我preSS装置后退按钮上LoginActivity。
But the problem is when i click device back button on the Login Activity it takes me to the ProfileActivity. I was expecting the application should close when i press device back button on LoginActivity.
我究竟做错了什么?
我还添加了安卓launchMode =singleTop
在清单中为我的 LoginActivity
I also added android:launchMode="singleTop"
in the manifest for my LoginActivity
感谢您
推荐答案
下面是一个解决方案,当您使用注销按钮来清除所有应用程序的活动。
Here is one solution to clear all your application's activities when you use the logout button.
您启动活动时,都会启动它是这样的:
Every time you start an Activity, start it like this:
Intent myIntent = new Intent(getBaseContext(), YourNewActivity.class);
startActivityForResult(myIntent, 0);
当你想关闭整个应用程序,做到这一点:
When you want to close the entire app, do this:
setResult(RESULT_CLOSE_ALL);
finish();
RESULT_CLOSE_ALL是一个唯一的整数最终的全局变量信号要关闭所有活动。
RESULT_CLOSE_ALL is a final global variable with a unique integer to signal you want to close all activities.
然后定义每一个活动的 onActivityResult(...)
回调,所以当一个活动返回与RESULT_CLOSE_ALL价值,这也叫完成()
:
Then define every activity's onActivityResult(...)
callback so when an activity returns with the RESULT_CLOSE_ALL value, it also calls finish()
:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch(resultCode)
{
case RESULT_CLOSE_ALL:
setResult(RESULT_CLOSE_ALL);
finish();
}
super.onActivityResult(requestCode, resultCode, data);
}
这将导致关闭所有的活动产生连带效应。
This will cause a cascade effect that closes all your activities.
这是一个黑客但是并使用 startActivityForResult
的方式,它不是设计来使用。
This is a hack however and uses startActivityForResult
in a way that it was not designed to be used.
也许一个更好的方式来做到这一点是使用广播接收器,如下所示:
Perhaps a better way to do this would be using broadcast receivers as shown here:
<一个href="http://stackoverflow.com/questions/3007998/on-logout-clear-activity-history-stack-$p$pventing-back-button-from-opening-l/3008684#3008684">On注销,明确活动历史堆栈,preventing&QUOT;返回]无法打开登录的,只有Activites
请参阅这些线程的其他方法还有:
See these threads for other methods as well:
这篇关于安卓:从后面堆栈中删除所有的previous活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!