Task和Back Stack之间有什么关系 [英] What is the relationship between Task and Back stack

查看:81
本文介绍了Task和Back Stack之间有什么关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我阅读android的官方文档时,我注意到android系统通过任务管理活动,但是它也使用back stack来控制活动序列,所以我的疑问是android系统是否使用单个back stack来进行活动.是控制活动顺序还是每个任务对应一个后退堆栈?

When I read the android official document, I notice that the Android system manages activities by task, but it also uses the back stack to control the activity sequence.So my doubt is that whether the android system uses a single back stack to control activities sequence or each task corresponding to a back stack?

推荐答案

任务只是应用程序所有实例化活动的集合.

A task is simply a collection of all the instantiated activities of an application.

例如:

如果我们拥有活动A1,A2和A3的应用程序A,并且在A1之后打开了A2,在A2之后打开了A3,则应用程序A的任务应如下所示:

If we have Application A with activities A1, A2 and A3 and A2 is opened after A1 and A3 is opened after A2, the task for Application A would look like this:

|A3|
|A2|
|A1|

现在,如果我们按返回按钮,将弹出A3并向用户显示A2.现在,应用程序A的任务应如下所示:

Now if we press the back button, A3 will get popped off and A2 will be revealed to the user. The task for Application A would now look like this:

|A2|
|A1|

如果我们按回去直到所有活动都消失,则该任务将被丢弃,并且下次启动应用程序A时,Android将创建一个新的任务,其中应用程序A的主要活动为第一个活动.

If we press back till all the activities are gone, then the task will be discarded and the next time we start Application A, Android will create a new task with Application A's main activity as the first activity.

现在,让我们以相同顺序再次打开A1,A2和A3.应用程序A的任务现在返回到:

Now, Let's open A1, A2 and A3 again in the same order. Application A's task is now back to:

|A3|
|A2|
|A1|

现在,假设我们按下主页按钮并启动另一个应用程序,即应用程序B.这将导致应用程序A的整个任务保留在后台,并为应用程序B创建一个新任务,并实例化其主要活动.所以现在我们有了这样的情况:

Now, suppose we press the home button and launch another app, Application B. This will cause Application A's entire task to be retained in the background and a new task is created for Application B with it's main activity instantiated. So now we have the situation that looks like this:

Application A       Application B
   |A3|                 |B1|
   |A2|
   |A1|

如果我们在应用程序B中打开更多活动,它们将被添加到任务中,就像应用程序A:

If we open more activities in Application B, they will be added to it's task just like Application A:

Application A       Application B
   |A3|                 |B3|
   |A2|                 |B2|
   |A1|                 |B1|

现在,如果我们切换回应用程序A,我们将把A的任务带到前台,而B的任务将保留在后台.

Now, if we switch back to Application A, we will bring A's task to the foreground and B's task will be retained in the background.

同一活动的多个实例也可以存在于同一任务上.这种行为可以得到控制.

Multiple instances of the same activity can also exist on the same task. This behavior can be controlled.

如果系统内存不足,它将开始在后台终止活动.如果任务的所有活动都消失了,则该任务也将被销毁.(更新:根据

If the system runs out of memory, it will start killing activities in the background. If all the activities of a task are gone, the task will also be destroyed. (UPDATE: According to this answer by Dianne Hackborn, it is not the individual activities but the entire process that hosts them that gets discarded. The docs may be a bit misleading in this regard and the confusion is yet to be resolved. I'll update it when I get more solid info.)

因此,总而言之,任务只是应用程序活动的集合.它用于维护应用程序所有实例化活动的堆栈"或后堆栈".当应用程序的所有活动都在后台时,它将保留在后台.当这些活动之一带回前台时,该任务也会被带回,并且当前活动的任务会被推到后台.如果系统需要内存,则可以销毁后台活动和任务.

So, to sum up, a task is simply a collection of activities of an application. It is used to maintain a "stack" or "backstack" of all the instantiated activities of an app. It gets retained in the background when the all the activities of an application are in the background. When one of these activities is brought back to the foreground, the task is brought back as well and the task of the current activity is pushed to the background. If the system needs memory, background activities and tasks can be destroyed.

官方文档提供了更多信息,我建议阅读:

The official docs give a lot more info and I recommend reading them:

http://developer.android.com/guide/components/tasks-and-back-stack.html

这篇关于Task和Back Stack之间有什么关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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