安卓:清空行业堆栈堆栈、清空、行业

2023-09-12 22:11:13 作者:争霸天涯

我有我的应用程序的一些活动。和流非常复杂。当我点击退出应用程序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);
 
狂热期退却,那些玩自动驾驶堆栈的公司还好吗

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

From the doc:

public static final int FLAG_ACTIVITY_CLEAR_TASK Added in API level 11

If 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 with FLAG_ACTIVITY_NEW_TASK.