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.一个应用通常包括多个activities.每个活动应该设计成用户能执行的某一种动作,并能启动其他的活动。比如,一个email应用可能有一个显示新的email 列表的活动,当用户选择一个email时,一个新的活动开启显示那个选择的email.

An activity can even start activities that exist in other applications on the device.一个activity甚至可以启动该设备上的其他应用中存在的活动。 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. 比如,如果你的应用想要发送一个邮件,你可以定义一个意图,执行一个'send'动作,并发送一些数据,包括像出件地址和信息.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). 在这种情况下,该意图将发送一封email,所一个email应用的"compose(写)"活动将启动(如果多个活动支持该相同的意图,那么系统将让用户选择使用那个).When the email is sent, your activity resumes and it seems as if the email activity was part of your application.当email被发送,你的活动将恢复,并且看起来email的活动好像是你的应用的一部分。 Even though the activities may be from different applications, Android maintains this seamless user experience by keeping both activities in the same task.即使活动来自不同的应用,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. 设备的Home屏幕是大多数任务开始的地方.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. 当用户触模应用运行器上的icon时(或者一个屏幕上的快捷方式),该应用的任务将出现在前景中.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.如果该应用没有任务存在(该应用最近没有使用),那么一个新的任务将创建,并且该应用的"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 key, 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). 当用户按BACK键时,堆顶的当前活动被弹出(该活动被销毁),并且前一个活动恢复(前一个活动的UI状态被恢复)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 key. 在堆中的活动不会被重新按排,只有压入及从堆中弹出--被当前活动启动时压入到堆,并且当用户用back离开它时,被弹出.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以时间线,量化了这个行为,显示了活动与当前堆在相应的时间上的每个点的过程.

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 key, the current activity is destroyed and the previous activity resumes.该图显示了一个任务的每个新活动,怎样以一个项的方式加入到返回堆上的。当用户按BACK键时,当前的活动被销毁,并且前一个活动恢复.

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.如果用户继续按BACK,那么堆中的每个活动将被弹出以显示前一个活动,直到用户返回到HOME屏幕(或者返回到该任务开始时正在运行的活动).当所有的活动从堆中移出,任务不再存在.

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 key.一个任务是一个紧密的单元,当用户开始一个新的任务或者去HOME 屏幕时,它可以被移到"后台" 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.当处理后台时,所有堆中的活动被停止了,但返回堆为任务保持它的原样---该任务只是简单的失去了,其他任务占据的焦点而已,如图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. 假设,有一个例子,当前任务(任务A),在它的堆中有三个任务,其中两个处于当前的活动之下.The user presses the HOME key, then starts a new application from the application launcher. When the Home screen appears, Task A goes into the background. 用户按了HOME键,然后从应用运行器开始了新的应用,当主屏幕出现时,任务A进入到了后台。When the new application starts, the system starts a task for that application (Task B) with its own stack of activities.当应用开始时,系统为应用(任务B)开启一个任务,该应用捅有自己的活动的堆. After interacting with that application, the user returns Home again and selects the application that originally started Task A. 当与该应用交互完后,用户再次返回到Home,并选择了原来已开启了任务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.现在,任务A出现在前景---所有该任务堆中的三个活动还是原样,并且堆顶的活动恢复. 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 key to reveal recent tasks and selecting one). 在这个时候,用户可以通过进入Home,并选择应用icon,开启那个任务,进入到任务B(或者通过触模并按住HOME键,显示最近的任务,并选择一个).This is an example of multitasking on Android.这是一个在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.后台可以同时保持多个任务,然而,如果用户同时运行多个后台任务,那么系统可能为了回收内存而开始删除后台活动,从而导致活动状态被丢失,关于活动状态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. 因此,如图3所示,你应用中的某个活动可能被多次实例化.As such, if the user navigates backward using the BACK key, each instance of the activity is revealed in the order they were opened (each with their own UI state). 也正因为如此,如果用户用BACK键导航返回,为了能打它们,该活动的每次实例化将被显示.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.但,如果你不想某个活动被多次实例化,你可以修改这个行为。怎样去做,在后面Managing Tasks关于管理任务一节讨论.

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 key while in Activity B, Activity A resumes with its state restored.当活动A启动活动B,活动A被停止,但系统记住了它的状态(比如卷曲位置和填入到表格的文本)。如果用户在活动B时,按了BACK键,活动A及它的状态会恢复.
  • When the user leaves a task by pressing the HOME key, 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键,离开了一个任务,当前的活动被停止并它的任务进入到了后台。系统会记住堆中每个活动的状态。后面,如果用户从运行器上选了一个ICON开启了该任务,这个任务将进入前台,并且堆顶的活动被恢复.
  • If the user presses the BACK key, 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.如果用户按了BACK键,当前的活动从堆中弹出并销毁,该堆中先前的活动被恢复。当一个活动被销毁,系统不会记信该活动的状态.
  • Activities can be instantiated multiple times, even from other tasks.活动可以被实例化多次,甚至可以从其他任务实例化多次.

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.更多关于如何保存活动的状态,请看Activities文档

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. Android管理任理任务和返回堆的方法如上所述---通过把所有连续启动的活动放在一个任务中,并放入后进先出的堆中---对于许多应用来说工作得很好,并且你不需要担心它们是怎样与任务连联起来的及在堆中是怎能样存在的.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.或者,当用户离开任务时,你想要清除你的返回堆中除根活动之外的所有活动.

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().通过在manifest文中使用<activity>元素的属性和你传给的intent标志,你可以做到这些及更其他的事情.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.在接下来的部分,你将明白如何使用这些manifest属性和意图标志来定义,活动怎样与任务关联及它们在返回堆中的行为。

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 key. Be sure to test for navigation behaviors that might conflict with the user's expected behavior.注意大多数应用不需要中断活动和任务的默认行为。如果你决定你的活动有必要改变默认的行为,要小心使用,并且确保测试活动运行期间的可用性,当用BACK键从其他任务及活动返回时的情况。并确保测试导航行为是否可能与用户所期望的行为相冲突。

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使用manifest文件

    When you declare an activity in your manifest file, you can specify how the activity should associate with tasks when it starts.当你在manifest文件中声明活动时,你可以指定该活动开始时,如何与任务关联。

  • 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可以在它的manifest中定义它是怎样与当前任务相关联的(或根不本),并且活动A也可以请求,活动B 应该与当前任务相关联。如果两个活动都定义了活动B 该怎样与当前任务相关联,那么活动A的请求(定义在意图中)优先于活动B的请求(定义在manifest中)

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.注意,一些运行模式可以在manifest 中使用,但不能当作一个意图标志使用,同理,一些运行模式可以当作意图标记使用,但不能在manifest文件中定义.

Using the manifest file使用manifest文件

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.当在你的manifest文件中声明一个活动时,你可以用<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该属性指定4种不同的运行模式.

"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.如果活动的一个实例存在于当前任务的顶部,则系统通调用它的 onNewIntent()方法路由意图给该实例,而不是创建该活动的新实例. 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).该活动可以被实例化多次,每次属于不同的任务,并一个任务可以有多个实例(但是只有当返回堆顶的活动,不存在该活动的实例的情况下)

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). 比如,假设一个任务的返回堆由根活动A及活动B,C,D构成.(这个堆是A-B-C-D;D是置顶)An intent arrives for an activity of type D. 一个意图要路由给活动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.如果D是默认运行模式"standard",则该类的新实例被运行,并且堆就变了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.然而,如果D的运行模式是"singleTop",则D存在的实例通过方法接收意图,因为它是在堆顶----该堆仍然保持是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".但,如果活动B的意图到了,那么即使B的运行模式是"singleTop"模式,仍会将B的新实例加入到堆上.

Note: When a new instance of an activity is created, the user can press the BACK key to return to the previous activity. But when an existing instance of an activity handles a new intent, the user cannot press the BACK key to return to the state of the activity before the new intent arrived in onNewIntent().注意当一个活动的新实例被创建时,用户可用BACK键返回到先前的活动.但当一个活动已存在的实例处理了一个新意图时,用户不能按BACK键返回到该新意图到来之前该活动的状态.

"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. 然而,如果活动的该活动的一个实例已存在于一个独立的任务中,系统通过调用它的 onNewIntent()方法把意图路由给存在的实例.Only one instance of the activity can exist at a time.该活动一次只能存在一个实例.

Note: Although the activity starts in a new task, the BACK key still returns the user to the previous activity.虽然该活动在一个新的任务开启的,但BACK仍返回给用户一个之前的活动

"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.另一个例子,Android的浏览器应用,声明浏览器的活动总是应该在它自己的任务中启动---通过在<activity>元素中指定"singleTask "运行模式 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.这表示,如果你的应用发出一个打开该Android浏览器的意图,你的应用的活动不是处于跟你的应用所处的相同的任务中. 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. 而是,要么为浏览器开启一个新的任务,或者,如果浏览器已经有一个任务在后台运行,则该任务将带到前台处理该新的意图.

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. BACK键总是把前一个活动带给用户,而不管该活动是否是在一个新任务中开启的,或者在同一个任务中开启的.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).然而,如果你在你的任务A中开启了一个活动,而该活动指定了"singleTask "运行模式,并且该活动在其所属的捅有返回堆的任务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. 在这种情况,当任务B进入前台处理该新的意图,BACK键在返回到任务A的顶部活动之前,首先导航返回到任务B的活动.Figure 4 visualizes this type of scenario.图4可视了该情景.

Figure 4. A representation of how an activity with launch mode "singleTask" is added to the back stack.表示了一个“singelTask”运行模式的活动是怎样加入到返回堆的. 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).如果该活动已是一个后台任务的一部分,并且捅有自己返回堆(任务B),那么该整个返回堆进入到前台,处于当前任务之上(任务A).

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.更多关于在manifest文件中使用运行模式的信息,看<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.注意,你通过该属性为你活动指定的行为,可以被开启你活动的活动,所带有的意图的标志所复盖,在下面讨论.

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(). 当开启一个活动时,你可以在传给startActivity()方法的意图中包含标志,修改活动与它的任务的默认关系。The flags you can use to modify the default behavior are:你可以用来修改默认行为的标志有:

FLAG_ACTIVITY_NEW_TASKStart 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_TOPIf 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()方法的调用.而不是创建该活动的新实例。(来接收onNewIntent()的调用)

This produces the same behavior as the "singleTop" launchMode value, discussed in the previous section.这和前面讨论的单个置顶"singleTop"运行模式launchMode值的行为表现是一样的.

FLAG_ACTIVITY_CLEAR_TOPIf 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.该标志经常与该标志一起结合使用,当他们一起使用时,这些标志是定位其他任务中存在的活动,并把它放到某个能响应该意图的位置的一个方法.

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. affinity 是指某个活动属于那个任务。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.这个属性是一个字串值,它必须是在元素中声明的默认包名中是唯一的(大概意思是唯一的值),因为系统将用这个名字为应用指定默认任务的亲密度关系.

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().通常,一个新的活动会运行在调用该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.然而,若传给startActivity()方法的意图,包含一个FLAG_ACTIVITY_NEW_TASK标志,系统将寻找一个不同的任务,宿主该新活动。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.如果已有一个存在的任务与这个新活动有同一个亲密度关系,该活动将在那个任务中运行。如果没有,则会开启一个新的任务.

    If this flag causes an activity to begin a new task and the user presses the HOME key to leave it, there must be some way for the user to navigate back to the task.如果这个标志导致某个活动开启了一个新的任务,并且用户按HOME键离开了它,那么必须有一些方法可以让用户能导航返回到该任务。 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().一些实体(比如通知管理器),总是在另一个任务中开启新活动的,从不会将把新的活动当成它自己的一部分,所以他们总是在传给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).如果你有一个活动,可以被外部实体使用这个标志调用,那么要注意,能让用户有一个独立的途径返回到该启动的任务。比如运行器图标(任务的根活动有一个CATEGORY_LAUNCHER意图过滤器.参考下面的Starting a task部分)

  • 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.它与同一个应用中的其他活动捅有同一个亲密度关系(默认应用亲密度)并且,它允许了re-parenting(即允许把自己改变到其他的任务中)这个属性。 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文件包括不此于一个"应用applicaion",那么你可能想用taskAffinity这个属性,为与该活动相关联的每个"应用applicaion",指定不同的亲密度关系。

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. 如果任务的根活动的这个属性是" true",任何时候用户离开了该任务并返回到该任务时,堆会被清除到根活动。 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.换句话说,就是它与 alwaysRetainTaskState属性相反.用户总是返回到该任务的初始状态,那怕离开了该任务一少会.
finishOnTaskLaunch
This attribute is like clearTaskOnLaunch, but it operates on a single activity, not an entire task. 这个属性与 clearTaskOnLaunch属性类似。便它的操作仅对于单个活动,而不是整个堆。It can also cause any activity to go away, including the root activity. 它可以导致包括根活动在内的所有活动,离开(go away).When it's set to "true", the activity remains part of the task only for the current session.当它设为"true"时,(仅用于当前会话的任务中的部分活动保留) If the user leaves and then returns to the task, it is no longer present.如果用户离开并返回到该任务,它不再出现.

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. 你可以把一个活动设置为某个任务的整个入点,通过使用意图过滤器android.intent.action.MAIN指定的动作和用android.intent.category.LAUNCHER指定分类.For example:例如

<activity ... >
<intent-filter ... >
<actionandroid:name="android.intent.action.MAIN"/>
<categoryandroid: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.基于这个原因,"singleTask"和"singleInstance"两个运行模式,应该用于有ACTION_MAINCATEGORY_LAUNCHER过滤器的活动 .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. 想像一下,如果丢失了该过滤 器会发生什么:一个意图运行了"singleTask"活动,初始化一个新任务,并且用户在该任务上处理了一些事情,然后按了HOME键. 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.由于该任务没有显示在应用运行器中,所以用户没有办法再返回该任务。

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"(参考"清除堆那里Clearing the stack")

更多相关文章

  1. Android中关于setTitle(CharSequence title)的知识总结
  2. Android8.0 Oreo新特性
  3. 配置android的adb环境变量
  4. Android应用架构 (Android(安卓)Dev Summit 2015)
  5. android 自动化测试 monkeyrunner
  6. Android(安卓)Studio运行项目提示Edit configuration
  7. Android(安卓)React Native安装指南
  8. Android(安卓)---Check System Version at Runtime(在软件运行时
  9. 【Android(安卓)Native Code开发系列】二 Ubuntu系统安装及软硬

随机推荐

  1. Android之layout属性介绍
  2. 玩转Android---UI篇---TextView(文本框)
  3. Android(安卓)APN开发流程分析
  4. Akita 一套Android快速开发库 发布1.2版
  5. Andriod AOA协议通信总结
  6. Android多Module合并aar打包
  7. 关于安装Android Studio的一些问题的解决
  8. 【译】Android开发指南(1)--什么是Androi
  9. android2.2 特性
  10. android控件的对齐方式(转)