Application Fundamentals--Activities and Tasks

Activities and Tasks

As noted earlier, one activity can start another, including one defined in a different application. Suppose, for example, that you'd like to let users display a street map of some location. There's already an activity that can do that, so all your activity needs to do is put together an Intent object with the required information and pass it to startActivity(). The map viewer will display the map. When the user hits the BACK key, your activity will reappear on screen.

翻译:之前我们说过一个 activity 可以启动另外一个activity,即使是其他应用中的activity也可以被启动 . 假定:你希望让你的应用能为用户显示一条街道的地图信息,那么在你的activity中,需要做的就是给startActivity()方法传入一个intent对象来激活一个map viewer activity,这个map viewer activity 将为用户显示一条街道的地图信息。当用户按下BACK按钮时候你的activity重新出现在屏幕上。

To the user, it will seem as if the map viewer is part of the same application as your activity, even though it's defined in another application and runs in that application's process. Android maintains this user experience by keeping both activities in the same task. Simply put, a task is what the user experiences as an "application." It's a group of related activities, arranged in a stack. The root activity in the stack is the one that began the task — typically, it's an activity the user selected in the application launcher. The activity at the top of the stack is one that's currently running — the one that is the focus for user actions. When one activity starts another, the new activity is pushed on the stack; it becomes the running activity. The previous activity remains in the stack. When the user presses the BACK key, the current activity is popped from the stack, and the previous one resumes as the running activity.

翻译:对于用户而言,看上去似乎map viewer是和前面一个activity都是这个应用程序的的组成部分,而事实上这个map viewer完全可能是归属另外一个应用的进程。 Android 能够提供这样的用户体验是通过把两个activities保持在同一个task中实现的. 简单地说,task是用户体验的“应用”,一个 task 可以理解为安放在一个堆栈中的一组相关联的activities,这个堆栈中的根activity是开始这个task的起点 — 这个activity也常常是用户在应用发射台选定的activity。当一个activity启动了另外一个新的activity, 这个新的activity就被压入堆栈,成为当前运行的activity. 先前那个activity 依然保留在堆栈中. 当用户按下 BACK 键, 当前运行的activity弹出堆栈,先前保留在堆栈中的那个activity重新变成当前运行的activity.

The stack contains objects, so if a task has more than one instance of the same Activity subclass open — multiple map viewers, for example — the stack has a separate entry for each instance. Activities in the stack are never rearranged, only pushed and popped.

翻译:堆栈用来存放activity对象,所以如果说一个task中有一个Activity的多个实例的话,比如打开了多个map viewers,每个map viewers实例都是分别压入当前task堆栈的,堆栈从不会对其中的实例顺序重新排列的,堆栈做的只是简单的压入和弹出实例。

A task is a stack of activities, not a class or an element in the manifest file. So there's no way to set values for a task independently of its activities. Values for the task as a whole are set in the root activity. For example, the next section will talk about the "affinity of a task"; that value is read from the affinity set for the task's root activity.

翻译:task是由一组 activities 实例组成的堆栈,它既不是类也不是manifest文件中的元素,所以,如果要修改或约定一个task的某些行为方式必须依赖这个task(堆栈)中的 activity。具体说是要通过task堆栈中的根activitiy来实现对当前task行为方式的特殊约定。比如task的affinity属性值就需要设定在根activity上。

All the activities in a task move together as a unit. The entire task (the entire activity stack) can be brought to the foreground or sent to the background. Suppose, for instance, that the current task has four activities in its stack — three under the current activity. The user presses the HOME key, goes to the application launcher, and selects a new application (actually, a new task). The current task goes into the background and the root activity for the new task is displayed. Then, after a short period, the user goes back to the home screen and again selects the previous application (the previous task). That task, with all four activities in the stack, comes forward. When the user presses the BACK key, the screen does not display the activity the user just left (the root activity of the previous task). Rather, the activity on the top of the stack is removed and the previous activity in the same task is displayed.

翻译:task中的所有activities是作为一个整体出现在前台或是退到后台,例如task1有四个Activity在该task的堆栈中,其他三个Activity在当前运行显示的Activity下面。如果这时用户按下HOME键,屏幕将显示应用发射台界面。然后用户选中另一个应用,实际上Android系统就开始了一个新的task2,这时候之前的那个task1便退到后台,用户这时候看到的task2的根Activity界面,此时,用户可能又按下HOME键,重新选中之前那个应用,这时候task1堆栈重新来到前台,这个task1有四个Activity在自己的堆栈中,此时用户可能又按下BACK键,此时刚才显示过的那个task2的根Activity界面是不会显示出来的,实际显示的将是task1的堆栈中上一次显示过的那个 Activity的界面。总结一下,这里我们要说明的是,任何一个task堆栈中的实例都是作为一个整体来到前台或是退到后台的,用户按下BACK键将只会导致前台所关联的那个task堆栈排列顺序发生变化。

The behavior just described is the default behavior for activities and tasks. But there are ways to modify almost all aspects of it. The association of activities with tasks, and the behavior of an activity within a task, is controlled by the interaction between flags set in the Intent object that started the activity and attributes set in the activity's <activity> element in the manifest. Both requester and respondent have a say in what happens.

翻译:以上描述的规则是activities和task的默认规则,但是这个规则不是说一成不变的,是可以被修改的。要修改这个规则需要在触发 Activity的intent对象中设定flag属性值或者是设定manifest文件中Activity组件元素的相关属性值。

针对这个规则的修改,可能用来设定Intent对象的flags属性的值主要是以下几个:

FLAG_ACTIVITY_NEW_TASK
FLAG_ACTIVITY_CLEAR_TOP
FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
FLAG_ACTIVITY_SINGLE_TOP

针对这个规则的修改,涉及到manifest文件中Activity组件元素的这几个属性:

taskAffinity
launchMode
allowTaskReparenting
clearTaskOnLaunch
alwaysRetainTaskState
finishOnTaskLaunch

The following sections describe what some of these flags and attributes do, how they interact, and what considerations should govern their use.

你可能感兴趣的:(android)