Android:清除活动堆栈 [英] Android: Clear Activity Stack

查看:23
本文介绍了Android:清除活动堆栈的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的申请中有几项活动.而且流程很复杂.当我点击注销应用程序导航到登录屏幕时,用户可以从那里通过取消按钮退出(调用 system.exit(0) )

I'm having several activities in my application. and flow is very complicated. When I click the Logout application navigates to login Screen and from there user can exit by cancel button (calling system.exit(0) )

当我退出或后退按钮时,系统从堆栈调用一个活动:(当我到达登录屏幕时如何清除堆栈中的所有活动?调用 finish() 是不切实际的,因为有很多活动,有些活动在活动时不应关闭,例如本机相机调用活动.

when I exit or back button, the 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
在 API 级别 11 中添加

public static final int FLAG_ACTIVITY_CLEAR_TASK
Added in API level 11

如果在传递给的 Intent 中设置Context.startActivity(),这个标志将导致任何现有的任务将与要清除的活动相关联活动开始.也就是说,活动成为一个新的根否则为空任务,任何旧活动均已完成.这个可以只能与 FLAG_ACTIVITY_NEW_TASK 结合使用.

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.

这篇关于Android:清除活动堆栈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆