同时关闭多个机器人活动 [英] Closing several android activities simultaneously

查看:99
本文介绍了同时关闭多个机器人活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序,你可以通过以下活动导航,直到活动栈颇深。

In my application you can navigate through several Activities until the Activity stack is quite deep.

我们希望在每一个活动的一个按钮,将带你直接回到主菜单 - 即弹出活动从栈中除了第一个

We'd like a button on every Activity that will take you straight back to the main menu - i.e. pop all Activities from the stack except the first one.

我已经放在一个视图,我可以很容易地把在应用程序中的每一个活动的按钮,但我无法弄清楚如何关闭一些活动一举。

I've put the button in a View that I can easily put on every Activity in the application, but I can't figure out how to close several Activities in one fell swoop.

(如果可能的话,这将是一件好事,如果视图可以工作多少个活动自行关闭 - 即检测出栈自己的活动是多么深)

(If possible, it would be good if the View could work out how many Activities to close by itself - i.e. detect how deep on the stack its own Activity is.)

推荐答案

看一看意图标志 FLAG_ACTIVITY_CLEAR_TOP 它说,它带来的有针对性的活动的顶部栈,除去一切可能已经在它上面。因此,使用该按钮可添加到您的所有活动,发动意图它的目标你的主菜单中,与该标志设置。

Have a look at the intent flag FLAG_ACTIVITY_CLEAR_TOP which says it brings the targeted activity to the top of the stack, removing everything else that might have been above it. So use that button you can add to all your activities to launch an intent which targets your main menu, with that flag set.

从文档:

如果设置,并且该活动是   推出设置在已经运行   当前的任务则代替,   推出的一个新实例   活性,所有的其他活动   在它的上面将被关闭,这   意图将被传递到(现在的   在上面),老年活动作为一个新的意图。

If set, and the activity being launched is already running in the current task, then instead of launching a new instance of that activity, all of the other activities on top of it will be closed and this Intent will be delivered to the (now on top) old activity as a new Intent.

例如,考虑一个任务   由活动:A,B,C,   D.若D与调用startActivity()   意图解析为组件   活动的B,然后是C和D会   完成和B接收定   意图,导致堆栈现在   是:A,B。

For example, consider a task consisting of the activities: A, B, C, D. If D calls startActivity() with an Intent that resolves to the component of activity B, then C and D will be finished and B receive the given Intent, resulting in the stack now being: A, B.

的当前运行的实例   在上面的例子中活性乙将   要么接受新的意图,你是   从这里开始,在其onNewIntent()   法,或者可以本身完成和   重新开始与新的意图。如果它   已宣布其推出模式是   多(默认),你有   没有设置FLAG_ACTIVITY_SINGLE_TOP在   相同的目的,那么这将是   成品和重新创建;对于所有其它   启动模式或者   FLAG_ACTIVITY_SINGLE_TOP被设置,那么   这个意图将被传递到   当前实例的onNewIntent()。

The currently running instance of activity B in the above example will either receive the new intent you are starting here in its onNewIntent() method, or be itself finished and restarted with the new intent. If it has declared its launch mode to be "multiple" (the default) and you have not set FLAG_ACTIVITY_SINGLE_TOP in the same intent, then it will be finished and re-created; for all other launch modes or if FLAG_ACTIVITY_SINGLE_TOP is set then this Intent will be delivered to the current instance's onNewIntent().

此发射模式也可用于   联用效果好   FLAG_ACTIVITY_NEW_TASK:如果用来   启动一个任务的根活动,它   会带来什么当前正在运行   该任务的实例   前台,然后将其清除其   根状态。这是特别有用,   例如,启动一个时   从通知活动   经理。

This launch mode can also be used to good effect in conjunction with FLAG_ACTIVITY_NEW_TASK: if used to start the root activity of a task, it will bring any currently running instance of that task to the foreground, and then clear it to its root state. This is especially useful, for example, when launching an activity from the notification manager.

这篇关于同时关闭多个机器人活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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