什么是任务并返回堆栈之间的关系 [英] what's the relationship between task and back stack

查看:165
本文介绍了什么是任务并返回堆栈之间的关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我看到Android的正式文件,我注意到了Android系统的任务管理活动,但它也使用回栈来控制活动sequence.So我的疑问是,Android系统使用一回栈来控制活动的序列或每个任务对应于一回栈?

When I read android official document,I notice the android system manage activities by task,but it also uses back stack to control the activity sequence.So my doubt is that android system use one 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和A2 A1和A3 A2后,打开后,打开应用程序A,为应用程序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|

现在,如果我们preSS返回按钮,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|

如果我们preSS回,直至所有的活动都没有了,那么任务将被丢弃,下次我们启动应用程序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|

现在,假设我们preSS home键,启动其他应用程序,应用程序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|

现在,如果我们切换到应用程序,我们会带来的任务推到前台和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.

如果系统内存不足,它会在后台启动秒杀活动。如果一个任务的所有活动都没有了,任务也将被销毁。 (更新:据<一href="http://stackoverflow.com/questions/7536988/android-app-out-of-memory-issues-tried-everything-and-still-at-a-loss/7576275#7576275">this回答通过戴安娜Hackborn,它不是个人的活动,但承载他们被丢弃的全过程。该文档可能有点误导在这方面的困惑是尚待解决。我会更新当我得到更坚实的信息。)

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 main a "stack" 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/组件/任务和 - 背stack.html

这篇关于什么是任务并返回堆栈之间的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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