安卓:清空行业堆栈 [英] Android: Clear Activity Stack
问题描述
我有我的应用程序的一些活动。和流非常复杂。当我点击退出应用程序naviagates登录屏幕,并从那里用户可以通过退出取消布顿(调用 system.exit(0)
)
当我退出或后退按钮,系统将调用从堆栈:(我怎么能清除栈中的所有活动当我到了登录界面的活动?调用完成()
是不实际的,因为有这么多的活动,当他们是活跃的一些活动不应该被关闭,如机摄像头调用活动。
的ValidateUser logoutuser =新的ValidateUser();
logoutuser.logOut();
意图loginscreen =新的意图(homepage.this,Login2.class);
(homepage.this).finish();
loginscreen.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(loginscreen);
大多数时候你就错了。如果你想不管里面有什么了关闭现有活动堆栈,并创建新的根,正确的一组标志如下:
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
<一个href="http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_CLEAR_TASK">From商务部:
公共静态最终诠释FLAG_ACTIVITY_CLEAR_TASK
新增的API级别11如果传递给一个Intent设置
Context.startActivity()
,该标志将导致任何现有的任务, 将与活性相关之前被清除 活动开始。即,活性变得一个新的根 否则空任务,任何旧的活动都结束了。这可 仅可配合使用FLAG_ACTIVITY_NEW_TASK
。
I'm having several activities in my application. and flow is very complicated. When I click the Logout application naviagates to login Screen and from there user can exit by cancel buton (calling system.exit(0)
)
when I exit or back button, system invokes an activity from stack :( how can I clear all the activities in the stack when i reach Login screen? calling finish()
is not practical as there are so many activities and some activities should no be closed when they are active such as native camera invoking activity.
validateuser logoutuser = new validateuser();
logoutuser.logOut();
Intent loginscreen = new Intent(homepage.this, Login2.class);
(homepage.this).finish();
loginscreen.setFlags( Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(loginscreen);
Most of you are wrong. If you want to close existing activity stack regardless of what's in there and create new root, correct set of flags is the following:
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
public static final int FLAG_ACTIVITY_CLEAR_TASK
Added in API level 11If set in an Intent passed to
Context.startActivity()
, this flag will cause any existing task that would be associated with the activity to be cleared before the activity is started. That is, the activity becomes the new root of an otherwise empty task, and any old activities are finished. This can only be used in conjunction withFLAG_ACTIVITY_NEW_TASK
.
这篇关于安卓:清空行业堆栈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!