码迷,mamicode.com
首页 > Windows程序 > 详细

API翻译 --- Tasks and Back Stack

时间:2016-07-19 10:24:47      阅读:253      评论:0      收藏:0      [点我收藏+]

标签:

IN THIS DOCUMENT

  1. Saving Activity State  保存activity状态

  2. Managing Tasks    管理任务

    1. Defining launch modes 定义启动模式

    2. Handling affinities 处理密切关系

    3. Clearing the back stack 清除回退栈

    4. Starting a task 启动任务

ARTICLES

  1. Multitasking the Android Way Android多任务方式

SEE ALSO

  1. Android Design: Navigation   Android设计:导航

  2. <activity> manifest element  清单元素

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.

一个应用程序通常包含多个Activity。每一个activity被设计执行一定的行为跳转到下个Activity。比如,一个email应用程序有一个显示新邮件的列表的activity。当用户选择一个email的时候,一个新的activity被打开,显示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.

一个Activity可以启动其他应用程序的activity.比如,如果你的应用程序想发送一个email,你可以定义一个意图去执行”发送“行为,其中可以包含一些数据,比如email的地址和信息。另一个应用程序的activity声明处理这个意图,并打开。这种情况下,目的是为了发送email,所以email应用程序组件的activity启动(如果多个activity支持这个意图,系统会让用户去选择使用哪一个)。当email被发送,你的activity恢复并且email的activity好像是你的应用的一部分。尽管这些activity是来自不同的应用。Android通过保持不同的activity在相同的任务栈,让用户有无缝的体验。

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.

当用户为了某种工作,彼此之间相互作用的时候,任务栈是activity的集合。activity被分配到栈里(回退栈),为了在栈里每个activity能按照顺序打开。

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.

一个设备的主屏幕是任务栈的起点。当用户触摸应用程序启动图标(或主屏幕的快捷方式),应用程序的任务栏显示前台。如果应用程序没有任务栈(应用程序当前没有被使用),新的任务栈会被创建,并且应用程序的主activity会打开,作为回退栈的根activity。

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.

当前的actvity打开其他的activity,新的activity推出到回退栈的顶部并且获得焦点。先前的回退栈保持在回退栈中,但是停止了。当这个activity停止的时候,系统保持着用户的当前状态。当用户点击回退按钮的时候,当前的activity从栈的顶端弹出(这个activity被销毁)并且先前的activity恢复(这个activity先前的界面被恢复)。activity在栈中不会被重新排列,只会从栈中压入和弹出。当前的activity启动时,压入栈顶。当用户离开点击返回按钮的时候弹出。这样,回退栈操作按照”后进 先出“的对象结构。图1 以时间线的方式展示了多个activity切换时对应当前时间点的回退栈情况。

技术分享

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

图1表示每一个新的activity都会添加到回退栈中。当用户点击返回按钮,当前的activity被销毁,先前的actvity恢复。

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.

如果用户不停的按下返回键,那么栈中的每一个回退栈都会被依次弹出,并显示前一个activity,直到用户回到home屏幕(或是启动这个任务栈的activity)。当所有的activity都从回退栈中移除,这个栈也就不存在了。

技术分享

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

任务栈B在前台接收用户交互。栈A在后台等待恢复。

技术分享

Figure 3. A single activity is instantiated multiple times.

一个activity被多次实例化。

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 theHome 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 Homebutton, 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 selecting the app‘s task from the recent appsscreen). This is an example of multitasking on Android.

任务栈是一个完整的单位,当用户启动一个新的任务栈,或通过home按钮返回到主界面的时候,这个任务栈移动到后台。当任务栈在后台的时候,里面所有的activity都处在停止状态。但是这个任务栈的回退栈仍然完整保留。-如图2。当其他栈获得焦点期间,当前栈只是失去焦点。任务栈可以回到焦点,以便用户继续之前的操作。例如,当前栈中有三个activity,--下面有两个activity。这时,用户按下home键,然后通过应用启动一个新的activity。当主界面出现的时候,任务栈A进入后台。当新的应用启动的时候,系统会重新启动一个任务栈B。用完这个应用后,用户再次返回主界面,并选中那个启动任务栈A的应用。这时候,任务栈A进入了前台,--栈中的三个activty依然完好,位于最顶端的actvity恢复运行。这时候,用户依然可以切换到任务栈B,通过返回主界面并选择对象的图标即可。(触摸并按住home键调用最近的任务栈列表并选中)。这是多任务栈的实例。

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.

Android系统可以在后台保持多个任务栈。但是,如果用户同时运行了多个后台任务栈,系统可能销毁后台activity用于释放内存。这种情况会导致activity状态的丢失。阅读actvity状态的部分。

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 pushed 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.

因为回退栈中的activity永远不会重新分配。如果应用允许某个Activiy可以启动多次,则新的activity会压入栈顶(而不是打开原来位于栈顶的activity)。于是,一个actvity可能初始化多次(甚至会位于不同的栈中)。如图3 ,用户使用返回键返回时,activity会按照原来打开的顺序显示出来。(用户界面也会按照原来的状态显示)。如果你不希望你的activity被多次实例化,你可以改变的它。具体的方法在后面的管理多个任务栈部分讲解。

To summarize the default behavior for activities and tasks:

总结activity和任务栈的默认行为:

  • 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.

  • 当ActivityA启动ActivityB的时候,ActvityA停止,但是系统保持它的状态(比如滚动条位置和form中填出的文字)。如果用户在actvityB界面中按返回键,Activity恢复它的储存状态。

  • 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按钮离开任务栈的时候,当前的activity停止并且它的任务栈跳到后台。系统在任务栈中保存每一个actvity的状态。如果用户后使用选择这个栈的图标来恢复任务栈,任务栈就会回到前台,栈顶的activity会恢复进行。

  • 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.

  • 如果用户按下返回键,当前的activity从栈中弹出,并销毁。栈中的前一个actvity恢复运行。当activity销毁的时候,系统不会保存actvity的状态。

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

  • activity可以在不同的时间,不同的的任务栈中被实例化多次。

Navigation Design  设计导航

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

应用导航如何工作的更多信息,阅读Android设计导航的指导。

Saving Activity State 保存activity状态


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.

如上所述,系统会在activity停止的时候保存其状态。这样,当用户返回时,activity显示的状态和它停止时一样。但是,你应该使用回调方法保存activity状态。

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.

当系统停止Activity(比如启动了一个新Activity或者Task 转入后台),系统在需要回收内存的时候有可能会完全销毁该Activity.这时,该Activity的状态信息将会丢失.就算这种情况发生,该Activity仍然会存在于Back Stack中.但是当它回到栈顶时,系统将必须重建它(而不是恢复).为了避免用户工作内容的丢失,你应通过实现Activity的 onSaveInstanceState()来主动保存这些内容。

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

关于如何保存Activity 状态的详情,请参阅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. 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.

如上所述,把所有已经启动的Activity相继放入同一个Task 中以及一个“后入先出”栈,Android 管理Task和Back Stack 的这种方式适用于大多数应用,你也不用去管理你的Activity 如何与Task关联及如何弹出Back Stack .不过,有时候你或许决定要改变这种普通的运行方式.也许你想让某个Activity启动一个新的Task(而不是被放入当前Task中),或者你想让 activity 启动时只是调出已有的某个实例(而不是在Back Stack 顶创建一个新的实例)或者你想在用户离开Task 时只保留根Activity,而Back Stack 中的其它 Activity 都要清空。

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() intent 中的标识即可。

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

这里 ,你可以使用<activity>的属性:

  • taskAffinity

  • launchMode

  • allowTaskReparenting

  • clearTaskOnLaunch

  • alwaysRetainTaskState

  • finishOnTaskLaunch

And the principal intent flags you can use are: 

你可以用到intent的标识

  • 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 属性和intent标识来定义Activity 与Task的关联性,以及 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 Backbutton. Be sure to test for navigation behaviors that might conflict with the user‘s expected behavior.

大多数应用不应该改变Activity和Task默认的工作方式。 如果你确定有必要修改默认方式,请保持谨慎,并确保Activity的启动和从其它Activity用返回键返回时的可用性.请确保对可能与用户预期的导航方式相冲突的地方进行测试.

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:

启动模式定义了一个新的Activity 实例与当前Task 的关联方式。定义启动模式的方法有两种:

  • 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.

    当你在 manifest文件中声明一个Activity 时,可以指定它启动时与Task 的关联方式。

  • Using Intent flags 使用Intent标记

    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()时,可以在Intent中包含一个标识,用于指明新Activity 是如何(是否)与当前Task 相关联。

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).

同样的,如果ActivityA启动了ActivityB,则 ActivityB可以在 manifest 中定义它如何与当之前的Task 关联(如果存在的话),并且,ActivityA 也可以要求 ActivityB 与当前 task 的关联关系.如果两个Activity都定义了,则 Activity A的请求(intent 中定义)会比Activity B 的定义(在 manifest 中)优先.

Note: Some launch modes available for the manifest file 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 文件中的某些启动模式在 intent 标识中并不可用,反之亦然,intent 中的某些模式也无法在 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.

在manifest文件中声明Activity时,你可以使用 <activity>元素的启动模式属性来指定Activity与Task的关系.

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:

启动模式属性指明了Activity启动Task的方式。启动模式属性可设置四种启动模式:

  • "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.

     默认的,每次访问实例化新的Activity,系统在启动Activity的Task中创建一个新的Activity 实例,并且把 intent 传送路径指向它.该Activity 可以被实例化多次,各个实例可以属于不同的Task,一个Task中也可以存在多个实例.

  • "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 activity at the top of the back stack is not an existing instance of the activity).

    如果actvity在当前栈的顶部,该系统就会使用 onNewIntent()方法通过intent 传递给已有实例。否则,还是实例化新的Activity.Activity可以被实例化多次,各个实例可以属于不同的Task,一个Task中可以存在多个实例(但只有Back Stack的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). 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".

    例如,假设Task的Back Stack中包含了根Activity A 和 Activities B、C、D(顺序是 A-B-C-D; D在栈顶). 这时候传过来的是启动D的intent,如果D的启动模式是默认的"standard",则会启动一个新的实例,栈的内容就会变为 A-B-C-D-D.但是,如果 D 的启动模式是 "singleTop",则已有的D的实例会通过 onNewIntent():接收这个 intent,因为该实例位于栈顶——栈中内容仍然维持 A-B-C-D 不变.当然,如果 intent 是要启动 B 的,则 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().

    注意: 一个Activity的新实例创建完毕后,用户可以按返回键返回前一个activity.但是当Activity 已有实例正在处理刚到达的intent 时,用户无法用返回键回到 onNewIntent()中 intent 到来之前的Activity 状态.

  • "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.

    系统创建一个新的任务栈并保证activity在新栈的顶部。但是如果这个栈在不同的栈中,则系统会通过调用其实例的 onNewIntent() 方法把 intent传给已有实例,而不是再创建一个新实例. 此 activity 同一时刻只能存在一个实例.

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

    注意: 虽然Activity启动了一个新的Task,但用户仍然可以用返回键返回前一个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.

     除了系统不会把其它Activity 放入当前实例所在的 Task 之外,其它均与"singleTask"相同,Activity总是它所在Task 的唯一成员;它所启动的任何Activity都会放入其它Task 中.

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的浏览器应用就是把web浏览器Activity声明为总是在它自己独立的Task中打开——把activity设为singleTask模式.这意味着,如果你的应用提交 intent 来打开 Android 的浏览器,则其 activity不会被放入你的应用所在的Task 中.取而代之的是,为浏览器启动一个新的Task;或是浏览器已经在后台运行,只要把Task 重新调入前台来处理新 intent 就可以.

Regardless of whether an activity starts in a new task or in the same task as the activity that started it, the Backbutton always takes the user to the previous activity. However, if you start an activity that specifies thesingleTask 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.

无论Activity是在一个新的Task中启动,还是位于其它已经存在的Task中,用户总是可以按返回键返回到前一个Activity 中.但是,如果你启动了一个启动模式设为singleTask的 activity,且有一个后台 task 中已存在实例的话,则这个后台 task 就会整个转到前台.这时,当前的Back Stack就包含了这个转入前台的Task 中所有的Activity,位置是在栈顶.图4所描述的就是这一种场景 

技术分享

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”的Activity 如何加入Back Stack 的示意.如果Activity 已经是在后台 Task 中并带有自己的Back Stack,则整个后台Back Stack 都会转入前台,并放入当前Task 的栈顶.

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.

注意: 你使用启动模式属性为Activity设置的模式可以被启动Activity的intent标识所覆盖,这将在下一章节具体说明。

Using Intent flags使用 Intent 标识

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:

在启动Activity 时,你可以传给startActivity()的 intent 中包含相应标识,用于修改Activity 与Task 的默认关系。这个标识可以修改的默认模式包括:

  • 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().

    在新的Task中启动Activity.如果要启动的Activity 已经运行于某个Task 中,则那个Task 将调入前台中,最后保存的状态也将会恢复,Activity 将在onNewIntent()中接收到这个新 intent.

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

    这个模式与前一章节所描述述的"singleTask"启动模式模式相同.

  • 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.

    如果要启动的Activity 就是当前Activity(位于Back Stack 顶),则已存在的实例将接收到一个onNewIntent()调用,而不是创建一个Activity 的新实例.

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

    这个模式与前一章节所述的 "singleTop"启动模式相同.

  • 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()).

    如果要启动的Activity已经在当前Task中运行,则不再启动一个新的实例,且所有在其上面的Activity将被销毁,然后通过onNewIntent()传入 intent 并恢复Activity(不在栈顶)的运行.

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

    此种模式在启动模式中没有对应的属性值.

    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 结合起来一起使用.这些标识定位在其它Task中已存在的Activity,再把它放入可以响应 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".

    注意:如果Activity 的启动模式配置为"standard",它就会先被移除出栈,再创建一个新的实例来处理这个 intent,因为启动模式为 "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.

    affinity指明Activity对于哪些Task亲和力更高.默认情况下,同一个应用中的所有 activity 都拥有同一个affinity 值. 因此,在同一应用程序中的所有Acttivity都喜欢在相同的Task.不过,你可以修改Activity 默认的 affinity 值.不同应用中的Activity 可以共享同一个affinity 值,同一个应用中的Activity也可以赋予不同的Task affinity值.

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

    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.

    你可以使用元素

    <activity>的任务亲密度属性修改Activity的affinity。任务亲密度属性是一个字符串值,必须与 <manifest> 元素定义的包名称保证唯一性,因为系统把这个包名称用于标识应用的默认Task affinity值.

    The affinity comes into play in two circumstances: 

    下面的两种情况下affinity将会发挥作用:
    • When the intent that launches an activity contains the FLAG_ACTIVITY_NEW_TASK flag.

      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 theFLAG_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.

      当启动Activity的intent包含了FLAG_ACTIVITY_NEW_TASK标志.

      默认情况下,一个新的Activity将被放入调用startActivity()的Activity 所在Task 中,且压入调用者所在的Back Stack 顶栈.不过,如果传给startActivity()的 intent 包含了FLAG_ACTIVITY_NEW_TASK标识,则系统会查找另一个Task并将新Activity 放入其中.一般情况下会新开一个任务,但并非一定如此.如果一个已有Task的affinity值与新 Activity的相同,则Activity 会放入该Task.如果没有,则会新建一个新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).

      如果这个标志导致Activity启动了一个新的Task并且用户按下Home键离开时,必须采取某种方式让用户能回到此Task.某些应用(比如通知管理器)总是让Activity放入其它Task 中启动,而不是放入自己的Task 中,因此,它们总是把FLAG_ACTIVITY_NEW_TASK 标识置入传给startActivity() 的 intent 中.如果你的Activity 可以被外部应用带此标识来启动,请注意用户会用其它方式返回启动Task,比如通过应用图标(Task 的根 Activity 带有一个CATEGORY_LAUNCHER intent过滤器;参阅下节#启动task).

    • When an activity has its allowTaskReparenting attribute set to "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.

      当一个Activity的 allowTaskReparenting 属性设置为 "true".

      在这种情况下,当某个Task 进入前台时,Activity 的affinity 值又与其相同,则它可以从启动时的Task 移入这个Task 中.例如:假设某旅游应用中有一个Activity 根据所选的城市来报告天气情况.它的affinity 与同一应用中的其它Activity 一样(整个应用默认的 affinity),且它允许重新指定此属性的归属,当你的另一个Activity 启动此天气预告Activity 时,它会在同一个Task 中启动.然而,当旅游应用的 Task 进入前台时,则天气报告Activity将会重新放入其Task中并显示出来.

    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文件中包含了多个“application”,从用户的角度来看,你可能想使用

     taskAffinity 属性来分配每个“application”中 activity 的 affinity 值.

    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.

    如果用户长时间离开某个Task,系统将会仅保留一个根Activity,而把其它Activity 都清除掉.当用户返回Task 时,只有根Activity 会被恢复.系统的这种行为,是因为经过了很长时间后,用户是要放弃之前进行的操作,返回Task 是为了开始新的操作.

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

    可以使用Activity的某些属性来改变这种行为:

    • 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.

      如果Task中根Activity 的此属性设为 "true" ,则默认的清理方式不会进行.即使过了很长一段时间,Task 中所有的Activity也还会保留在栈中.

    • 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.

      如果Task 中根Activity 的此属性设为 "true",则只要用户离开并再次返回该 Task,栈就会被清理至根Activity。也就是说,正好与 alwaysRetainTaskState 相反.用户每次返回Task时看到的都是初始状态,即使只是离开一会儿.

    • 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,只是它只对一个 Activity有效,不是整个Task.这能让任何一个Activity 消失,包括 根Activity.如果Activity 的此属性设为 "true",则只会保留Task中当前session所涉及的内容.如果用户离开后再返回Task,它就不存在.

    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"、category 为 "android.intent.category.LAUNCHER"发送 intent来指定某个Activity为Task的入口,例如:

    <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.

    这种intent过滤器将会让此Activity的图标和标签作为应用启动图标来显示,用户可以启动此Activity,并且在之后任何时候返回其启动时的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 aCATEGORY_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.

    第二个能力非常重要.用户必须能离开一个Task ,之后能再回来使用这个启动Task 的Activity.由于这个原因,Activity每次都会使用这两种启动模式"singleTask" 和 ""singleInstance"启动Task。并且仅用于ACTION_MAIN 和CATEGORY_LAUNCHER 过滤器的Activity才能使用.

    如果缺少过滤器会发生怎样的问题:某个intent启动了一个 "singleTask" activity。新建了一个Task,用户在此Task中工作了一段时间.然后他按了Home键,Task 就转入后台,变为不可见状态,这时用户就无法再回到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,将<activity> 元素的finishOnTaskLaunch设置为"true" 即可.详细请(参阅Clearing the stack)

API翻译 --- Tasks and Back Stack

标签:

原文地址:http://blog.csdn.net/mikky_android/article/details/51943674

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!