`

【翻译】(6)任务与后退堆栈

 
阅读更多

【翻译】(6)任务与后退堆栈

 

see

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

 

原文见

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

 

-------------------------------

 

Tasks and Back Stack

 

任务与后退堆栈

 

-------------------------------

 

Quickview

 

快速概览

 

* All activities belong to a task 

 

* 所有活动属于一个任务

 

* A task contains a collection of activities in the order in which the user interacts with them

 

* 一个任务包含一组带有用户界面的有序活动

 

* Tasks can move to the background and retain the state of each activity in order for users to perform other tasks without losing their work

 

* 任务可以移到后台并保持每个活动的状态,以便让用户执行另一个任务而不会丢失他们的工作。

 

In this document

 

本文目录

 

* Saving Activity State 保存活动状态

* Managing Tasks 管理任务

* Defining launch modes 定义启动模式

* Handling affinities 处理关系

* Clearing the back stack 清除后退堆栈

* Starting a task 启动任务

 

Articles

 

文章

 

Multitasking the Android Way

 

Android的多任务手段

 

See also

 

另见

 

Android Design: Navigation

 

Android设计:导航

 

Application Lifecycle video 

 

应用程序生命周期视频

 

<activity> manifest element 

 

<activity>清单元素

 

-------------------------------

 

An application usually contains multiple activities. Each activity should be designed around a specific kind of action the user can perform and can start other activities. For example, an email application might have one activity to show a list of new email. When the user selects an email, a new activity opens to view that email.

 

应用程序通常包含多个活动。每个活动应该围绕用户可执行的一种特定类型动作进行设计,并能启动其它活动。例如,一个电子邮件应用程序可能拥有展示一列新电子邮件的活动。当用户选择一个电子邮件,会打开新的活动以便查看电子邮件。

 

An activity can even start activities that exist in other applications on the device. For example, if your application wants to send an email, you can define an intent to perform a "send" action and include some data, such as an email address and a message. An activity from another application that declares itself to handle this kind of intent then opens. In this case, the intent is to send an email, so an email application's "compose" activity starts (if multiple activities support the same intent, then the system lets the user select which one to use). When the email is sent, your activity resumes and it seems as if the email activity was part of your application. Even though the activities may be from different applications, Android maintains this seamless user experience by keeping both activities in the same task.

 

一个活动甚至可以启动存在于设备上的其它应用程序的活动。例如,如果你的应用程序想发送电子邮件,你可以定义一个意图去执行send动作并包含一些数据,诸如邮箱地址和消息。然后,打开声明它自己以处理此类意图的来自另一个应用程序的活动。这种情况下,意图是用来发送邮件,所以电子邮件应用程序的compose活动启动(如果多个活动支持同一个意图,那么系统让用户选择使用哪一个)。当电子邮件被发送时,你的活动恢复,并且好像电子邮件活动就是你的应用程序的一部分那样。尽管活动可以来自不同的应用程序,但Android通过在同一个任务中保持这两个活动以维护这种无缝的用户体验。

 

A task is a collection of activities that users interact with when performing a certain job. The activities are arranged in a stack (the "back stack"), in the order in which each activity is opened.

 

一个任务是一组活动,用户在执行某些工作时用这些活动与用户交互。活动被安排成一个堆栈(后退堆栈),以每个活动被打开的顺序进行排列。

 

The device Home screen is the starting place for most tasks. When the user touches an icon in the application launcher (or a shortcut on the Home screen), that application's task comes to the foreground. If no task exists for the application (the application has not been used recently), then a new task is created and the "main" activity for that application opens as the root activity in the stack.

 

设备(注:Android手机)的桌面屏幕放在大多数任务的起始位置。当用户触碰应用程序启动器中的一个图标(或桌面屏幕的快捷方式),应用程序的任务就会跑到前台。如果不存在该应用程序的任务(该应用程序最近没有被使用)。那么新的任务被创建并打开应用程序的main活动,作为堆栈的根活动。

 

When the current activity starts another, the new activity is pushed on the top of the stack and takes focus. The previous activity remains in the stack, but is stopped. When an activity stops, the system retains the current state of its user interface. When the user presses the BACK button, the current activity is popped from the top of the stack (the activity is destroyed) and the previous activity resumes (the previous state of its UI is restored). Activities in the stack are never rearranged, only pushed and popped from the stack—pushed onto the stack when started by the current activity and popped off when the user leaves it using the BACK button. As such, the back stack operates as a "last in, first out" object structure. Figure 1 visualizes this behavior with a timeline showing the progress between activities along with the current back stack at each point in time.

 

在当前活动启动另一个活动时,新活动被压进堆栈栈顶并获取焦点。前一个活动保留在堆栈中,但被停止。当一个活动停止,系统维护其用户界面的当前状态。当用户按下后退按钮,当前活动从堆栈栈顶弹出(活动被销毁)并恢复前一个活动(恢复它之前的用户界面状态)。堆栈中的活动永远不会被重新排列,只会在堆栈中压入和弹出——在当前活动启动时压进堆栈,在用户用后退按钮离开它时弹出堆栈。因此,后退堆栈的行为就像一个后进先出的对象结构。图1可视化了这一行为,用时间轴显示每个时间点上活动与当前后退堆栈之间的演变。

 

-------------------------------

 

(图略:

1. 前台活动:活动1

   后退堆栈

1->2 启动活动2

2. 前台活动:活动2

   活动1

2->3 启动活动3

3. 前台活动:活动3

   活动2 活动1

3->5 导航后退

4. 前台活动:活动3 <- 活动3被销毁 

5. 前台活动:活动2

   活动1

 

Figure 1. A representation of how each new activity in a task adds an item to the back stack. When the user presses the BACK button, the current activity is destroyed and the previous activity resumes.

 

图1. 表示一个任务中每个新活动如何添加一个条目到后退堆栈中。当用户按下后退按钮时,当前活动被销毁并恢复前一个活动。

 

-------------------------------

 

If the user continues to press BACK, then each activity in the stack is popped off to reveal the previous one, until the user returns to the Home screen (or to whichever activity was running when the task began). When all activities are removed from the stack, the task no longer exists.

 

如果用户继续按后退键,那么堆栈中的活动逐一被弹出以显露前一个活动,直至用户返回到桌面屏幕(注:Home屏)(或返回到在任务开始时正在运行的那个活动)。当所有活动从堆栈中移除,堆栈将不再存在。

 

-------------------------------

 

(图略:

(左图)任务B:

前台活动:活动Z

活动Y

(右图)任务A:

后台:活动Y 活动X

 

Figure 2. Two tasks: Task B receives user interaction in the foreground, while Task A is in the background, waiting to be resumed.

 

图2. 两个任务:任务B接收前台中的用户交互,此时任务A正处于后台中,等待被恢复。

 

-------------------------------

 

(图略:

后退堆栈:

自下而上:桌面活动,活动2,桌面活动,活动1

单独的实例(指向两个Home活动)

 

Figure 3. A single activity is instantiated multiple times.

 

图3. 一个单一活动被多次实例化。

 

-------------------------------

 

A task is a cohesive unit that can move to the "background" when users begin a new task or go to the Home screen, via the HOME button. While in the background, all the activities in the task are stopped, but the back stack for the task remains intact—the task has simply lost focus while another task takes place, as shown in figure 2. A task can then return to the "foreground" so users can pick up where they left off. Suppose, for example, that the current task (Task A) has three activities in its stack—two under the current activity. The user presses the HOME button, then starts a new application from the application launcher. When the Home screen appears, Task A goes into the background. When the new application starts, the system starts a task for that application (Task B) with its own stack of activities. After interacting with that application, the user returns Home again and selects the application that originally started Task A. Now, Task A comes to the foreground—all three activities in its stack are intact and the activity at the top of the stack resumes. At this point, the user can also switch back to Task B by going Home and selecting the application icon that started that task (or by touching and holding the HOME button to reveal recent tasks and selecting one). This is an example of multitasking on Android.

 

一个任务是一个粘合单元,可以在用户启动一个新堆栈或通过按HOME按钮跑到桌面屏幕时移到后台。在后台中,任务的所有活动被停止,但任务的后退堆栈维持完整——当另一个任务发生时这个任务简单地失去焦点,正如图2所示。之后任务可以返回到前台以使用户能重新获得它们离开的地方。例如,假设当前任务(任务A)在它的堆栈中有三个活动——两个在当前活动的下面。用户按下HOME按钮,然后从应用程序启动器中启动一个新应用程序。当桌面屏幕显示时,任务A跑到后台。当启动新的应用程序时,系统为应用程序启动一个任务(任务B),使用它自己的活动堆栈。在与应用程序交互之后,用户再次返回到桌面并选择原来启动任务A的应用程序。现在,任务A返回前台——在它的堆栈中所有三个活动是完好的,并且恢复堆栈栈顶的活动。此时,用户还可以通过跑到桌面选择启动那个任务的应用程序图标,以切换回任务B(或通过触碰并按住HOME按钮以显示最近任务并选择其中的一个)。这是Android多任务的一个示例。

 

-------------------------------

 

Note: Multiple tasks can be held in the background at once. However, if the user is running many background tasks at the same time, the system might begin destroying background activities in order to recover memory, causing the activity states to be lost. See the following section about Activity state.

 

注意:多个任务可以同时被后台持有。然而,如果用户正在同时运行很多后台任务,系统可能开始销毁后台活动以便回收内存,导致活动的状态被丢弃。参看下面关于活动状态的章节。

 

-------------------------------

 

Because the activities in the back stack are never rearranged, if your application allows users to start a particular activity from more than one activity, a new instance of that activity is created and popped onto the stack (rather than bringing any previous instance of the activity to the top). As such, one activity in your application might be instantiated multiple times (even from different tasks), as shown in figure 3. As such, if the user navigates backward using the BACK button, each instance of the activity is revealed in the order they were opened (each with their own UI state). However, you can modify this behavior if you do not want an activity to be instantiated more than once. How to do so is discussed in the later section about Managing Tasks.

 

因为后退堆栈中的活动从不被重新排列,所以如果你的应用程序允许用户从多于一个活动中启动一个特定活动,那个活动的新实例被创建并往上压进(注:弹出?,这里的意思可能是说,看上去好像向上移动了)堆栈(而非把活动之前的某个实例移到栈顶),正如图3所示。因此,你的应用程序的活动可能被实例化多次(甚至来自不同的任务),如图3所示。因此,如果用户使用后退按钮导航后退,那么活动的每个实例以它们曾经被打开的顺序展现(每个实例都有它们自己的用户界面状态)。然而,如果你不想让一个活动被实例化多次,你可以修改这一行为,在后面关于任务管理的章节中将讨论如何做到这一点。

 

To summarize the default behavior for activities and tasks:

 

总而言之,活动和任务的默认行为是:

 

* When Activity A starts Activity B, Activity A is stopped, but the system retains its state (such as scroll position and text entered into forms). If the user presses the BACK button while in Activity B, Activity A resumes with its state restored.

 

* 当活动A启动活动B时,活动A被停止,但系统保持它的状态(诸如滚动条位置和输入到表单中的文本)。如果用户在活动B时按下后退按钮,那么活动A恢复执行并恢复它原有状态。

 

* When the user leaves a task by pressing the HOME button, the current activity is stopped and its task goes into the background. The system retains the state of every activity in the task. If the user later resumes the task by selecting the launcher icon that began the task, the task comes to the foreground and resumes the activity at the top of the stack.

 

* 当用户通过按HOME按钮离开任务时,当前活动被停止并且它的任务转到后台。系统保持堆栈中每个活动的状态。如果之后用户通过选择曾经启动过某任务的启动器图标恢复任务时,任务跑到前台并恢复栈顶的活动。

 

* If the user presses the BACK button, the current activity is popped from the stack and destroyed. The previous activity in the stack is resumed. When an activity is destroyed, the system does not retain the activity's state.

 

* 如果用户按下后退按钮,那么当前活动弹出堆栈并销毁。堆栈中前一个活动被恢复。当一个活动被销毁,系统不再保持活动的状态。

 

* Activities can be instantiated multiple times, even from other tasks.

 

活动可以被多次实例化,甚至来自其它任务。

 

-------------------------------

 

Navigation Design

 

导航设计

 

For more about how app navigation works on Android, read Android Design's Navigation guide.

 

想获取更多关于Android上的应用导航是如何工作的信息,请阅读Android设计的导航指引。

 

-------------------------------

 

Saving Activity State

 

保存活动状态

 

As discussed above, the system's default behavior preserves the state of an activity when it is stopped. This way, when users navigate back to a previous activity, its user interface appears the way they left it. However, you can—and should—proactively retain the state of your activities using callback methods, in case the activity is destroyed and must be recreated.

 

正如上面谈论的那样,系统的默认行为是活动停止的时候会保持活动的状态。这样,当用户导航后退到前一个活动时,它的用户界面表现他们离开的方式。然而,你可以——并且应该——主动使用回调方法保持你的活动的状态,如果活动被销毁并且必须被重新创建的话。

 

When the system stops one of your activities (such as when a new activity starts or the task moves to the background), the system might destroy that activity completely if it needs to recover system memory. When this happens, information about the activity state is lost. If this happens, the system still knows that the activity has a place in the back stack, but when the activity is brought to the top of the stack the system must recreate it (rather than resume it). In order to avoid losing the user's work, you should proactively retain it by implementing the onSaveInstanceState() callback methods in your activity.

 

当系统停止你其中一个活动时(诸如在新活动启动或任务移到后台时),如果它需要回收系统内存,那么系统可能完全地销毁那个活动。当发生这种情况时,活动的相关信息被丢失。如果发生这种情况,系统仍知道该活动在堆栈中占据一个位置,但当活动被带到栈顶时系统必须重新创建它(而非恢复它)。为了避免丢失用户的工作,你应该主动通过在你的活动中实现onSaveInstanceState()回调方法来保持它。

 

For more information about how to save your activity state, see the Activities document.

 

更多关于如何保存你的活动状态的信息,请参考活动文档。

 

Managing Tasks

 

管理任务

 

The way Android manages tasks and the back stack, as described above—by placing all activities started in succession in the same task and in a "last in, first out" stack—works great for most applications and you shouldn't have to worry about how your activities are associated with tasks or how they exist in the back stack. However, you might decide that you want to interrupt the normal behavior. Perhaps you want an activity in your application to begin a new task when it is started (instead of being placed within the current task); or, when you start an activity, you want to bring forward an existing instance of it (instead of creating a new instance on top of the back stack); or, you want your back stack to be cleared of all activities except for the root activity when the user leaves the task.

 

Android管理任务和后退堆栈的方式,正如上面所说——通过把启动成功的所有活动放置到同一个任务并放进一个后进先出的堆栈——对于大多数应用程序来说工作得很好,你不必担心你的活动如何与任务关联或它们如何存在于后退堆栈中。然而,你可以决定你希望打破这种通常的行为。可能你希望你的应用程序的一个活动在它开始时启动一个新任务(而非放进当前任务);或者,当你启动一个活动时,你希望把它现存的实例带到前面(而非在后退堆栈的顶部创建新实例);或者当用户离开任务时,你希望你的后退堆栈清空一切活动,除了根活动。

 

You can do these things and more, with attributes in the <activity> manifest element and with flags in the intent that you pass to startActivity().

 

你可以通过使用<activity>清单元素中的属性和在你传递给startActivity()的意图中使用标志,做到这些事情甚至更多。

 

In this regard, the the principal <activity> attributes you can use are:

 

关于这一点,你可以使用的主要<activity>属性有:

 

taskAffinity

launchMode

allowTaskReparenting

clearTaskOnLaunch

alwaysRetainTaskState

finishOnTaskLaunch

 

And the principal intent flags you can use are:

 

而你可以使用的主要意图标志有:

 

FLAG_ACTIVITY_NEW_TASK

FLAG_ACTIVITY_CLEAR_TOP

FLAG_ACTIVITY_SINGLE_TOP

 

In the following sections, you'll see how you can use these manifest attributes and intent flags to define how activities are associated with tasks and how the behave in the back stack.

 

在以下章节中,你将看到你可以如何使用这些清单属性和意图标志去定义活动如何与任务关联以及在后退堆栈中如何行动。

 

-------------------------------

 

Caution: Most applications should not interrupt the default behavior for activities and tasks. If you determine that it's necessary for your activity to modify the default behaviors, use caution and be sure to test the usability of the activity during launch and when navigating back to it from other activities and tasks with the BACK button. Be sure to test for navigation behaviors that might conflict with the user's expected behavior.

 

警告:大多数应用程序不应该打破活动和任务的默认行为。如果你确定你的应用必须修改默认行为,请小心使用并确保测试过,在启动期间,以及使用后退按钮从另一个活动和任务导航回到它时,活动的可用性。确保测试过可能与用户所期待的行为有冲突的导航行为。

 

-------------------------------

 

Defining launch modes

 

定义启动模式

 

Launch modes allow you to define how a new instance of an activity is associated with the current task. You can define different launch modes in two ways:

 

启动模式允许你定义活动的新实例是如何关联到当前任务的。你可以用两种方式定义不同的启动模式:

 

* Using the manifest file

 

* 使用清单文件

 

When you declare an activity in your manifest file, you can specify how the activity should associate with tasks when it starts.

 

当你在你的清单文件中声明一个活动时,你可以指定活动在其启动时应该如何与任务关联。

 

* Using Intent flags

 

* 使用意图标志

 

When you call startActivity(), you can include a flag in the Intent that declares how (or whether) the new activity should associate with the current task.

 

当你调用startActivity()时,你可以在意图中包含标志以声明新活动如何(或是否)与当前任务关联。

 

As such, if Activity A starts Activity B, Activity B can define in its manifest how it should associate with the current task (if at all) and Activity A can also request how Activity B should associate with current task. If both activities define how Activity B should associate with a task, then Activity A's request (as defined in the intent) is honored over Activity B's request (as defined in its manifest).

 

因此,如果活动A启动活动B,活动B可以在它的清单中定义它如何与当前任务关联(如果有),而活动A还可以请求活动B应该如何与当前任务关联。如果两个活动都定义了活动B应该如何与任务关联,那么活动A的请求(在意图中定义)优于活动B的请求(在它的清单中定义)

 

-------------------------------

 

Note: Some the launch modes available in the manifest are not available as flags for an intent and, likewise, some launch modes available as flags for an intent cannot be defined in the manifest.

 

注意:清单中一些可用的启动模式作为意图的标志是不可用的,同样,一些作为意图的标志可用的启动模式不能定义在清单中。

 

-------------------------------

 

Using the manifest file

 

使用清单文件

 

When declaring an activity in your manifest file, you can specify how the activity should associate with a task using the <activity> element's launchMode attribute.

 

当在你的清单文件中声明一个活动时,你可以使用<activity>元素的launchMode属性指定活动应该如何与任务关联。

 

The launchMode attribute specifies an instruction on how the activity should be launched into a task. There are four different launch modes you can assign to the launchMode attribute:

 

launchMode属性指定一种关于活动应该如何启动成一个任务的指令。你可以为launchMode属性赋予四种不同的启动方式:

 

* "standard" (the default mode) (默认模式)

 

Default. The system creates a new instance of the activity in the task from which it was started and routes the intent to it. The activity can be instantiated multiple times, each instance can belong to different tasks, and one task can have multiple instances.

 

默认值。系统创建以活动开始的任务中的活动新实例并把意图路由给它。活动可以被实例化多次,每个实例可以属于不同的任务,而一个任务可以拥有多个实例(注:多个活动实例)。

 

* "singleTop"

 

If an instance of the activity already exists at the top of the current task, the system routes the intent to that instance through a call to its onNewIntent() method, rather than creating a new instance of the activity. The activity can be instantiated multiple times, each instance can belong to different tasks, and one task can have multiple instances (but only if the the activity at the top of the back stack is not an existing instance of the activity).

 

如果一个活动实例总是存在于当前堆栈栈顶,那么系统通过调用它的onNewIntent()方法,把意图路由至那个实例,而非创建活动的新实例。活动可以被实例化多次,每个实例可以属于不同任务,而一个任务可以拥有多个实例(注:多个活动实例)(但仅当后退堆栈栈顶的活动不是活动的已存在实例时)。

 

For example, suppose a task's back stack consists of root activity A with activities B, C, and D on top (the stack is A-B-C-D; D is on top). An intent arrives for an activity of type D. If D has the default "standard" launch mode, a new instance of the class is launched and the stack becomes A-B-C-D-D. However, if D's launch mode is "singleTop", the existing instance of D receives the intent through onNewIntent(), because it's at the top of the stack—the stack remains A-B-C-D. However, if an intent arrives for an activity of type B, then a new instance of B is added to the stack, even if its launch mode is "singleTop".

 

例如,假设一个任务的后退堆栈由根活动A、活动B、C和栈顶的D组成(堆栈为A-B-C-D;D在栈顶)。意图的目标是类型D的活动。如果D拥有默认的standard启动模式,那么该类的新实例被启动使堆栈变成A-B-C-D-D。然而,如果D的启动模式是singleTop,那么已存在的D实例通过onNewIntent()接收到意图,因为它在栈顶——堆栈仍然是A-B-C-D。然而,如果意图的目标是类型B的活动,那么B的新实例被添加到堆栈,即使它的启动模式是singleTop。

 

-------------------------------

 

Note: When a new instance of an activity is created, the user can press the BACK button to return to the previous activity. But when an existing instance of an activity handles a new intent, the user cannot press the BACK button to return to the state of the activity before the new intent arrived in onNewIntent().

 

注意:当活动的一个新实例被创建时,用户可以按后退按钮返回前一个活动。但当活动的现存实例处理一个新意图时,用户不能按后退按钮返回到在onNewIntent()内新意图到达之前的活动状态。

 

-------------------------------

 

* "singleTask"

 

The system creates a new task and instantiates the activity at the root of the new task. However, if an instance of the activity already exists in a separate task, the system routes the intent to the existing instance through a call to its onNewIntent() method, rather than creating a new instance. Only one instance of the activity can exist at a time.

 

系统创建一个新任务并实例化新任务的根活动。然而,如果活动的实例已经存在于一个单独任务中,那么系统通过调用它的onNewIntent()方法,把意图路由至现存实例,而非创建一个新实例。同一时间只能存在一个活动实例。

 

-------------------------------

 

Note: Although the activity starts in a new task, the BACK button still returns the user to the previous activity.

 

注意:虽然活动在新任务中启动,但后退按钮仍然可以把用户返回到前一个活动。

 

-------------------------------

 

* "singleInstance".

 

Same as "singleTask", except that the system doesn't launch any other activities into the task holding the instance. The activity is always the single and only member of its task; any activities started by this one open in a separate task.

 

和singleTask相同,但不同的是系统不会启动其它任意活动进去持有该实例的任务。活动总是单一的,并且只是它的任务的成员;任意由它启动的活动在独立的任务中打开。

 

As another example, the Android Browser application declares that the web browser activity should always open in its own task—by specifying the singleTask launch mode in the <activity> element. This means that if your application issues an intent to open the Android Browser, its activity is not placed in the same task as your application. Instead, either a new task starts for the Browser or, if the Browser already has a task running in the background, that task is brought forward to handle the new intent.

 

作为另一个示例,Android浏览器应用程序声明网页浏览器活动应该总是在它自己的任务中打开——通过在<activity>元素中指定singleTask启动模式。这意味着如果你的应用程序发送意图去打开Android浏览器,那么它的活动并不是放在和你的应用程序相同的任务中。取而代之,不管是启动新任务以获得浏览器,还是,如果浏览器已经有一个正在运行于后台的任务,那个任务都会被带到前面去处理新意图。

 

Regardless of whether an activity starts in a new task or in the same task as the activity that started it, the BACK button always takes the user to the previous activity. However, if you start an activity that specifies the singleTask launch mode, then if an instance of that activity exists in a background task, that whole task is brought to the foreground. At this point, the back stack now includes all activities from the task brought forward, at the top of the stack. Figure 4 illustrates this type of scenario.

 

不管是在新任务中,还是在与启动它的活动相同的堆栈中启动活动,后退按钮总是把用户带到前一个活动。然而,如果你启动一个活动,它指定singleTask启动模式,那么如果一个活动实例存在于一个后台任务中,整个任务就会被带到前台。在这一点上,后退堆栈现在包含来自被向前带的任务的所有活动,放在堆栈的顶部。图4描绘此类场景。

 

-------------------------------

 

(图略:

1. 后退堆栈 前台活动:活动2

   后退堆栈:活动1

   后台活动:活动Y (<- 用"singleTask"启动模式声明)活动X

1->2 启动活动Y

2. 后退堆栈 前台活动:活动Y  

   后退堆栈:活动X 活动2 活动1

2->3 导航后退

3. 后退堆栈 前台活动:活动X  

   后退堆栈:活动2 活动1 

3->4 导航后退

3. 后退堆栈 前台活动:活动2  

   后退堆栈:活动1 

 

Figure 4. A representation of how an activity with launch mode "singleTask" is added to the back stack. If the activity is already a part of a background task with its own back stack, then the entire back stack also comes forward, on top of the current task.

 

图4. 表示运行模式为singleTask的活动如何被添加到后退堆栈。如果活动已经是拥有自己私有后退堆栈的后台任务的一部分,那么整个后退堆栈也会向前移,放到当前任务的上方。

 

-------------------------------

 

For more information about using launch modes in the manifest file, see the <activity> element documentation, where the launchMode attribute and the accepted values are discussed more.

 

更多关于在清单文件中使用启动模式的信息,请参见<activity>元素文档,那里会讨论更多关于launchMode属性和可接受值的问题。

 

-------------------------------

 

Note: The behaviors that you specify for your activity with the launchMode attribute can be overridden by flags included with the intent that start your activity, as discussed in the next section.

 

注意:你用launchMode属性为你的活动指定的行为可以被启动活动的意图所包含的标志覆盖,正如下面章节讨论的那样。

 

-------------------------------

 

Using Intent flags

 

使用意图标志

 

When starting an activity, you can modify the default association of an activity to its task by including flags in the intent that you deliver to startActivity(). The flags you can use to modify the default behavior are:

 

当启动一个活动时,你可以通过在传递给startActivity()的意图中包含标志,修改一个活动与它的任务的默认关系。你可以用来修改默认行为的标志有:

 

* FLAG_ACTIVITY_NEW_TASK

 

Start the activity in a new task. If a task is already running for the activity you are now starting, that task is brought to the foreground with its last state restored and the activity receives the new intent in onNewIntent().

 

在新任务中启动活动。如果一个任务已经为你现在启动的活动而运行,那么该任务被带到前台恢复它上一次的状态,并且活动在onNewIntent()中接收新意图。

 

This produces the same behavior as the "singleTask" launchMode value, discussed in the previous section.

 

它产生与singleTask的launchMode值相同的行为,在前面的章节中曾经讨论过。

 

* FLAG_ACTIVITY_SINGLE_TOP

 

If the activity being started is the current activity (at the top of the back stack), then the existing instance receives a call to onNewIntent(), instead of creating a new instance of the activity.

 

如果正在启动的活动是当前活动(在后退堆栈栈顶),那么现存实例接收对onNewIntent()的调用,而非创建活动的新实例。

 

This produces the same behavior as the "singleTop" launchMode value, discussed in the previous section.

 

它产生与singleTop的launchMode值相同的行为,在前面的章节中曾经讨论过。

 

* FLAG_ACTIVITY_CLEAR_TOP

 

If the activity being started 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 are destroyed and this intent is delivered to the resumed instance of the activity (now on top), through onNewIntent()).

 

如果正在被启动的活动总是在当前任务中运行,那么不是启动活动的新实例,而是销毁其顶部的其它所有活动,并通过onNewIntent()把这个意图传递给被恢复的活动实例(现在在栈顶)。

 

There is no value for the launchMode attribute that produces this behavior.

 

没有一种launchMode属性值可以产生这种行为。

 

FLAG_ACTIVITY_CLEAR_TOP is most often used in conjunction with FLAG_ACTIVITY_NEW_TASK. When used together, these flags are a way of locating an existing activity in another task and putting it in a position where it can respond to the intent.

 

FLAG_ACTIVITY_CLEAR_TOP最常和FLAG_ACTIVITY_NEW_TASK一起组合使用。当一起使用时,这些标志是定位另一个任务内的现存活动并把它放到它能响应意图的地方的方式。

 

-------------------------------

 

Note: If the launch mode of the designated activity is "standard", it too is removed from the stack and a new instance is launched in its place to handle the incoming intent. That's because a new instance is always created for a new intent when the launch mode is "standard".

 

注意:如果特定活动的启动模式是standard,那么它还会从任务中移除并且在它的位置上启动新的实例以处理传入的意图。那是因为当启动模式是standard时,新实例总是为了新意图而被创建。

 

-------------------------------

 

Handling affinities

 

处理关系

 

The affinity indicates which task an activity prefers to belong to. By default, all the activities from the same application have an affinity for each other. So, by default, all activities in the same application prefer to be in the same task. However, you can modify the default affinity for an activity. Activities defined in different applications can share an affinity, or activities defined in the same application can be assigned different task affinities.

 

关系指示一个活动倾向于属于哪个任务。默认,相同应用程序的所有活动拥有同一个关系。所以默认下,相同应用程序中的所有活动倾向于放在相同的任务中。然而,你可以修改一个活动的默认关系。定义在不同应用程序中的活动可以共享一个关系,或者定义在同一个应用程序中的活动可以被赋予不同的任务关系。

 

You can modify the affinity for any given activity with the taskAffinity attribute of the <activity> element.

 

你可以使用<activity>元素的taskAffinity属性修改任意给定活动的关系。

 

The taskAffinity attribute takes a string value, which must be unique from the default package name declared in the <manifest> element, because the system uses that name to identify the default task affinity for the application.

 

taskAffinity属性携带一个字符串值,它必须不同于在<manifest>元素中声明的默认包名,因为系统使用那个名称为应用程序标识默认的任务关系。

 

The affinity comes into play in two circumstances:

 

关系起作用有两种情况:

 

* When the intent that launches an activity contains the FLAG_ACTIVITY_NEW_TASK flag.

 

* 当启动活动的意图包含FLAG_ACTIVITY_NEW_TASK标志。

 

A new activity is, by default, launched into the task of the activity that called startActivity(). It's pushed onto the same back stack as the caller. However, if the intent passed to startActivity() contains the FLAG_ACTIVITY_NEW_TASK flag, the system looks for a different task to house the new activity. Often, it's a new task. However, it doesn't have to be. If there's already an existing task with the same affinity as the new activity, the activity is launched into that task. If not, it begins a new task.

 

默认,新活动在调用startActivity()的活动的同一个任务中启动。它作为调用方被压进相同的后退堆栈。然而,如果传递给startActivity()的意图包含FLAG_ACTIVITY_NEW_TASK标志,那么系统寻找不同的任务去容纳新的活动。通常,是一个新任务。然而,并非一定是这样。如果已经有一个现存任务,和新活动具有相同的关系,那么活动在那个任务中启动。如果没有,它才会启动新任务。

 

If this flag causes an activity to begin a new task and the user presses the HOME button to leave it, there must be some way for the user to navigate back to the task. Some entities (such as the notification manager) always start activities in an external task, never as part of their own, so they always put FLAG_ACTIVITY_NEW_TASK in the intents they pass to startActivity(). If you have an activity that can be invoked by an external entity that might use this flag, take care that the user has a independent way to get back to the task that's started, such as with a launcher icon (the root activity of the task has a CATEGORY_LAUNCHER intent filter; see the Starting a task section below).

 

如果这个标志导致一个活动启动新任务,而用户按HOME按钮离开,那么必须有一些方法让用户导航回到任务。一些实体(诸如通知管理器)总是在外部任务中启动活动,从不作为它们自身的一部分,所以它们总是放置FLAG_ACTIVITY_NEW_TASK在传递给startActivity()的意图中。如果你有一个可以被外部实体调用的活动可能使用了这个标志,请注意用户有一种独立的方法返回它启动的任务,诸如用一个启动器图标(任务的根活动有CATEGORY_LAUNCHER意图过滤器;参见下面启动任务的章节)。

 

* When an activity has its allowTaskReparenting attribute set to "true".

 

* 当活动的allowTaskReparenting属性被设置为true。

 

In this case, the activity can move from the task it starts to the task it has an affinity for, when that task comes to the foreground.

 

这种情况下,当那个任务跑到前台时,活动可以从它启动的任务中移动到与它有密切关系的任务中。

 

For example, suppose that an activity that reports weather conditions in selected cities is defined as part of a travel application. It has the same affinity as other activities in the same application (the default application affinity) and it allows re-parenting with this attribute. When one of your activities starts the weather reporter activity, it initially belongs to the same task as your activity. However, when the travel application's task comes to the foreground, the weather reporter activity is reassigned to that task and displayed within it.

 

例如,假设一个报道选中城市的天气状况的活动被定义为一个旅游应用程序的一部分。它与同一个应用程序中其它活动拥有相同的关系(默认的应用程序关系)并且允许用这个属性重新定义父级(注:父级任务)。当你的其中一个活动启动天气报道器的活动时,它最初属于和你的活动相同的堆栈。然而,当旅游应用程序的堆栈跑到前台时,天气报道器活动会被重新赋予到那个任务并在它里面显示。

 

-------------------------------

 

Tip: If an .apk file contains more than one "application" from the user's point of view, you probably want to use the taskAffinity attribute to assign different affinities to the activities associated with each "application".

 

提示:如果一个.apk文件从用户的视角看包含多于一个“应用程序”,你可能希望使用taskAffinity属性,赋予不同关系给与每个“应用程序”关联的活动。

 

-------------------------------

 

Clearing the back stack

 

清除后退堆栈

 

If the user leaves a task for a long time, the system clears the task of all activities except the root activity. When the user returns to the task again, only the root activity is restored. The system behaves this way, because, after an extended amount of time, users likely have abandoned what they were doing before and are returning to the task to begin something new.

 

如果用户长时间离开一个任务,系统清除任务中的所有活动,除了根活动。当用户再次返回到任务,只有根活动被恢复。系统以这种方式行动,是因为在被延长的较长时间后,用户可能已经放弃他们之前正在做的事情并返回到那个任务去做新的事情。

 

There are some activity attributes that you can use to modify this behavior:

 

你可以使用一些活动属性去更改这种行为:

 

* alwaysRetainTaskState

 

If this attribute is set to "true" in the root activity of a task, the default behavior just described does not happen. The task retains all activities in its stack even after a long period.

 

如果在任务的根活动中这个属性被设置为true,上面描述的默认行为不会发生。任务保持堆栈中的所有活动,即便在长时间后。

 

* clearTaskOnLaunch

 

If this attribute is set to "true" in the root activity of a task, the stack is cleared down to the root activity whenever the user leaves the task and returns to it. In other words, it's the opposite of alwaysRetainTaskState. The user always returns to the task in its initial state, even after a leaving the task for only a moment.

 

如果在任务的根活动中这个属性被设置为true,那么不管用户何时离开任务并返回到它,任务都会向下清除直至根活动。换言之,它与alwaysRetainTaskState相反。用户总是以任务的初始状态返回到它,即便仅仅离开任务一阵子。

 

* finishOnTaskLaunch

 

This attribute is like clearTaskOnLaunch, but it operates on a single activity, not an entire task. It can also cause any activity to go away, including the root activity. When it's set to "true", the activity remains part of the task only for the current session. If the user leaves and then returns to the task, it is no longer present.

 

这个属性类似clearTaskOnLaunch,但它操作在单一活动上,而非整个任务。它也可能导致任意活动离开,包括根活动在内。当它被设置为true时,活动仅为当前会话保持活动中的部分。如果用户离开然后又返回到任务,那么它将不再存在。

 

Starting a task

 

开始一个任务

 

You can set up an activity as the entry point for a task by giving it an intent filter with "android.intent.action.MAIN" as the specified action and "android.intent.category.LAUNCHER" as the specified category. For example:

 

你可以通过为一个活动指定意图过滤器,配置它作为任务的入口点,用android.intent.action.MAIN作为指定动作并且用android.intent.category.LAUNCHER作为指定分类。例如:

 

-------------------------------

 

<activity ... >

    <intent-filter ... >

        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />

    </intent-filter>

    ...

</activity>

 

-------------------------------

 

An intent filter of this kind causes an icon and label for the activity to be displayed in the application launcher, giving users a way to launch the activity and to return to the task that it creates any time after it has been launched.

 

此类意图过滤器导致活动的图标和标签被显示在应用程序的启动器中,给予用户一种启动活动和返回到任务的方式,在它被启动之后能随时创建它。

 

This second ability is important: Users must be able to leave a task and then come back to it later using this activity launcher. For this reason, the two launch modes that mark activities as always initiating a task, "singleTask" and ""singleInstance", should be used only when the activity has an ACTION_MAIN and a CATEGORY_LAUNCHER filter. Imagine, for example, what could happen if the filter is missing: An intent launches a "singleTask" activity, initiating a new task, and the user spends some time working in that task. The user then presses the HOME button. The task is now sent to the background and is not visible. Now the user has no way to return to the task, because it is not represented in the application launcher.

 

第二个功能十分重要:用户必须有能力离开任务然后在之后使用其活动启动器来返回到它。因此,两种被标记为singleTask和singleInstance的活动的启动模式,总是初始化一个任务,仅当活动拥有ACTION_MAIN和CATEGORY_LAUNCHER过滤器。请设想一下,例如,如果缺少过滤器的话可能发生的情况:一个意图启动一个singleTask的活动,初始化一个新任务,然后用户花一些时间在任务中工作。然后用户按下HOME按钮。任务现在被发送到后台并且不再可见。现在用户无法返回到任务,因为它并没有被呈现在应用程序启动器中。

 

For those cases where you don't want the user to be able to return to an activity, set the <activity> element's finishOnTaskLaunch to "true" (see Clearing the stack).

 

对于那些你不愿意让用户能返回一个活动的情况,请设置<activity>元素的finishOnTaskLaunch属性为true(参看清除堆栈)。

 

Except as noted, this content is licensed under Apache 2.0. For details and restrictions, see the Content License.

 

除特别说明外,本文在Apache 2.0下许可。细节和限制请参考内容许可证。

 

Android 4.0 r1 - 27 Oct 2011 21:16

 

Android 4.0 r1 - 20 Jan 2012 22:25

 

-------------------------------

patch:

1. Android 4.0 r1 - 20 Jan 2012 22:25

(1)

Back key

->

Back button

 

(2)

Figure 2. Two tasks: Task A is in the background, waiting to be resumed, while Task B receives user interaction in the foreground.

->

Figure 2. Two tasks: Task B receives user interaction in the foreground, while Task A is in the background, waiting to be resumed.

 

(3)

Home key

->

Home button

 

(4)

++

Navigation Design

For more about how app navigation works on Android, read Android Design's Navigation guide.

 

(5)

Regardless of whether an activity starts in a new task or in the same task as the activity that started it, the BACK key always takes the user to the previous activity. However, 

if you start an activity from your task (Task A) that specifies the singleTask launch mode, then that activity might have an instance in the background that belongs to a task with its own back stack (Task B). In this case, when Task B is brought forward to handle a new intent, the BACK key first navigates backward through the activities in Task B before returning to the top-most activity in Task A. Figure 4 visualizes this type of scenario.

->

Regardless of whether an activity starts in a new task or in the same task as the activity that started it, the BACK button always takes the user to the previous activity. However, 

if you start an activity that specifies the singleTask launch mode, then if an instance of that activity exists in a background task, that whole task is brought to the foreground. At this point, the back stack now includes all activities from the task brought forward, at the top of the stack. Figure 4 illustrates this type of scenario.

 

(6)

Figure 4. A representation of how an activity with launch mode "singleTask" is added to the back stack. If the activity is already a part of a background task with its own back stack (Task B), then the entire back stack also comes forward, on top of the current task (Task A).

->

Figure 4. A representation of how an activity with launch mode "singleTask" is added to the back stack. If the activity is already a part of a background task with its own back stack, then the entire back stack also comes forward, on top of the current task.

 

(7)

This second ability is important: Users must be able to leave a task and then come back to it later using this activity launcher. For this reason, the two launch modes that mark activities as always initiating a task, "singleTask" and ""singleInstance", should be used only when the activity has an ACTION_MAIN and a CATEGORY_LAUNCHER filter. Imagine, for example, what could happen if the filter is missing: An intent launches a "singleTask" activity, initiating a new task, and the user spends some time working in that task. The user then presses the HOME key. 

The task is now sent to the background and not visible. Because it is not represented in the application launcher, the user has no way to return to the task.

->

This second ability is important: Users must be able to leave a task and then come back to it later using this activity launcher. For this reason, the two launch modes that mark activities as always initiating a task, "singleTask" and ""singleInstance", should be used only when the activity has an ACTION_MAIN and a CATEGORY_LAUNCHER filter. Imagine, for example, what could happen if the filter is missing: An intent launches a "singleTask" activity, initiating a new task, and the user spends some time working in that task. The user then presses the HOME button. 

The task is now sent to the background and is not visible. Now the user has no way to return to the task, because it is not represented in the application launcher.

(8)

图基本上都被修改过

 

-------------------------------

 

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

 

(此页部分内容基于Android开源项目,以及使用根据创作公共2.5来源许可证描述的条款进行修改)


分享到:
评论

相关推荐

    电子商务之价格优化算法:梯度下降:机器学习在价格优化中的角色.docx

    电子商务之价格优化算法:梯度下降:机器学习在价格优化中的角色.docx

    ToadforOracle与Oracle数据库版本兼容性教程.docx

    ToadforOracle与Oracle数据库版本兼容性教程.docx

    browser360-cn-stable-13.3.1016.4-1-amd64.deb

    360浏览器银河麒麟版 for X86 适配兆芯 / 海光 / intel / AMD CPU

    基于React.js和Material-UI个人作品集网站模板(附源码+说明文档).zip

    使用React.js构建,提供多种主题可供选择,并且易于定制。该项目旨在帮助开发者和自由职业者创建自己的个性化投资组合。 主要功能点 多种主题可供选择,包括绿色、黑白、蓝色、红色、橙色、紫色、粉色和黄色 易于定制,可以在src/data文件夹中更新个人信息 包含主页、关于、简历、教育、技能、经验、项目、成就、服务、推荐信、博客和联系等多个部分 支持通过Google表单收集联系信息 提供SEO优化建议 支持多种部署方式,如Netlify、Firebase、Heroku和GitHub Pages 技术栈主要 React.js Material-UI Axios React-fast-marquee React-helmet React-icons React-reveal React-router-dom React-router-hash-link React-slick Slick-carousel Validator

    中小型企业财务管理系统 SSM毕业设计 附带论文.zip

    中小型企业财务管理系统 SSM毕业设计 附带论文 启动教程:https://www.bilibili.com/video/BV1GK1iYyE2B

    apsw-3.38.5.post1-cp39-cp39-win_amd64.whl.rar

    python whl离线安装包 pip安装失败可以尝试使用whl离线安装包安装 第一步 下载whl文件,注意需要与python版本配套 python版本号、32位64位、arm或amd64均有区别 第二步 使用pip install XXXXX.whl 命令安装,如果whl路径不在cmd窗口当前目录下,需要带上路径 WHL文件是以Wheel格式保存的Python安装包, Wheel是Python发行版的标准内置包格式。 在本质上是一个压缩包,WHL文件中包含了Python安装的py文件和元数据,以及经过编译的pyd文件, 这样就使得它可以在不具备编译环境的条件下,安装适合自己python版本的库文件。 如果要查看WHL文件的内容,可以把.whl后缀名改成.zip,使用解压软件(如WinRAR、WinZIP)解压打开即可查看。 为什么会用到whl文件来安装python库文件呢? 在python的使用过程中,我们免不了要经常通过pip来安装自己所需要的包, 大部分的包基本都能正常安装,但是总会遇到有那么一些包因为各种各样的问题导致安装不了的。 这时我们就可以通过尝试去Python安装包大全中(whl包下载)下载whl包来安装解决问题。

    电子商务之价格优化算法:线性回归:价格优化策略实施.docx

    电子商务之价格优化算法:线性回归:价格优化策略实施.docx

    工业数字化转型的关键技术及其应用场景解析

    内容概要:报告详细介绍了企业数字化转型的驱动因素、数字化转型方案分类及其应用场景,重点关注了云计算、超连接、数字孪生、人工智能、分布式账本、增材制造、人机接口、数据共享、工业物联网等关键技术。这些技术不仅支持了企业的运营效率提升和业务模式创新,也为实现更快、更开放、更高效的数字化转型提供了支撑。报告最后提出了企业实施数字化转型的六个步骤。 适合人群:企业高级管理人员、技术人员、咨询顾问,以及对工业数字化转型感兴趣的读者。 使用场景及目标:帮助企业制定和实施数字化转型策略,优化运营模式,提升业务效率,增强市场竞争力。同时,也可作为政府部门、研究机构和行业协会的参考文献。 其他说明:报告中提到的关键技术及其应用场景对企业数字化转型具有重要的指导意义,特别是对于那些希望通过数字化转型实现业务创新和升级的企业。

    基于java的线上选课系统的设计与实现答辩PPT.pptx

    基于java的线上选课系统的设计与实现答辩PPT.pptx

    原版aggdraw-1.3.15-cp311-cp311-win_arm64.whl-下载即用直接pip安装.zip

    安装前的准备 1、安装Python:确保你的计算机上已经安装了Python。你可以在命令行中输入python --version或python3 --version来检查是否已安装以及安装的版本。 个人建议:在anaconda中自建不同python版本的环境,方法如下(其他版本照葫芦画瓢): 比如创建python3.8环境,anaconda命令终端输入:conda create -n py38 python==3.8 2、安装pip:pip是Python的包管理工具,用于安装和管理Python包。你可以通过输入pip --version或pip3 --version来检查pip是否已安装。 安装WHL安装包 1、打开命令行(或打开anaconda命令行终端): 在Windows上,你可以搜索“cmd”或“命令提示符”并打开它。 在macOS或Linux上,你可以打开“终端”。 2、cd到whl文件所在目录安装: 使用cd命令导航到你下载的whl文件所在的文件夹。 终端输入:pip install xxx.whl安装即可(xxx.whl指的是csdn下载解压出来的whl) 3、等待安装完成: 命令行会显示安装进度,并在安装完成后返回提示符。 以上是简单安装介绍,小白也能会,简单好用,从此再也不怕下载安装超时问题。 使用过程遇到问题可以私信,我可以帮你解决! 收起

    电子商务之价格优化算法:贝叶斯定价:贝叶斯网络在电子商务定价中的应用.docx

    电子商务之价格优化算法:贝叶斯定价:贝叶斯网络在电子商务定价中的应用.docx

    IMG_20241105_235746.jpg

    IMG_20241105_235746.jpg

    基于java的毕业设计选题系统答辩PPT.pptx

    基于java的毕业设计选题系统答辩PPT.pptx

    专升本考试资料全套.7z

    专升本考试资料全套.7z

    Trustwave DbProtect:数据库活动监控策略制定.docx

    Trustwave DbProtect:数据库活动监控策略制定.docx

    VB程序实例-CD-ROM开关.zip

    基于VB的程序实例,可供参考学习使用

    课设毕设基于SpringBoot+Vue的教育资源共享平台源码可运行.zip

    本压缩包资源说明,你现在往下拉可以看到压缩包内容目录 我是批量上传的基于SpringBoot+Vue的项目,所以描述都一样;有源码有数据库脚本,系统都是测试过可运行的,看文件名即可区分项目~ |Java|SpringBoot|Vue|前后端分离| 开发语言:Java 框架:SpringBoot,Vue JDK版本:JDK1.8 数据库:MySQL 5.7+(推荐5.7,8.0也可以) 数据库工具:Navicat 开发软件: idea/eclipse(推荐idea) Maven包:Maven3.3.9+ 系统环境:Windows/Mac

    基于Thinkphp5框架的Java插件设计源码

    该源码项目是一款基于Thinkphp5框架的Java插件设计,包含114个文件,其中Java源文件60个,PNG图片32个,XML配置文件7个,GIF图片7个,Git忽略文件1个,LICENSE文件1个,Markdown文件1个,Xmind文件1个,Idea项目文件1个,以及JAR文件1个。

    数据库开发和管理最佳实践.pdf

    数据库开发和管理最佳实践.pdf

    课设毕设基于SpringBoot+Vue的农场投入品运营线上管理系统源码可运行.zip

    本压缩包资源说明,你现在往下拉可以看到压缩包内容目录 我是批量上传的基于SpringBoot+Vue的项目,所以描述都一样;有源码有数据库脚本,系统都是测试过可运行的,看文件名即可区分项目~ |Java|SpringBoot|Vue|前后端分离| 开发语言:Java 框架:SpringBoot,Vue JDK版本:JDK1.8 数据库:MySQL 5.7+(推荐5.7,8.0也可以) 数据库工具:Navicat 开发软件: idea/eclipse(推荐idea) Maven包:Maven3.3.9+ 系统环境:Windows/Mac

Global site tag (gtag.js) - Google Analytics