Application Fundamentals--Launch modes-启动模式
Launch modes-启动模式
There are four different launch modes that can be assigned to an <activity> element's launchMode attribute:翻译:activity元素的launchMode属性可以设定为以下四种值:
"standard" (the default mode)
"singleTop"
"singleTask"
"singleInstance"
The modes differ from each other on these four points:---launchMode属性设定为以上这四个值会导致在以下在四个方面出现不同的差异:
* 一、Which task will hold the activity that responds to the intent ---哪个task将持有被intent激活的这个Activity(实例被压入当前task堆栈还是其他堆栈)?. For the "standard" and "singleTop" modes, it's the task that originated the intent (and called startActivity()) — unless the Intent object contains the FLAG_ACTIVITY_NEW_TASK flag. In that case, a different task is chosen as described in the previous section, Affinities and new tasks.
翻译: "standard" 和 "singleTop"启动模式的Activity组件被激活时候将归属当前task。除非激活这个Activity的intent中的flag值被设定为FLAG_ACTIVITY_NEW_TASK。
In contrast, the "singleTask" and "singleInstance" modes mark activities that are always at the root of a task. They define a task; they're never launched into another task.
翻译:"singleTask" 和 "singleInstance" 启动模式的Activity组件被激活时候始终是一个task的根Activity,这样的Activity是不可能进入其他task堆栈(当前task 堆栈)的,因为它始终是扮演根Activity的角色,如果它进入当前task堆栈的话,它就无法扮演根Activty的角色。所以这样的 Activity实例总是作为根Activity的角色归属于一个自身的task堆栈。
*
二、Whether there can be multiple instances of the activity---是否该Activity组件可以有多个实例存在?. A "standard" or "singleTop" activity can be instantiated many times. They can belong to multiple tasks, and a given task can have multiple instances of the same activity.
翻译:"standard" 和 "singleTop"启动模式的Activity组件可以被多次实例化。在不同的task堆栈中可以重复出现,在一个的task堆栈中也可以有多个同类实例,
In contrast, "singleTask" and "singleInstance" activities are limited to just one instance. Since these activities are at the root of a task, this limitation means that there is never more than a single instance of the task on the device at one time.
翻译:相反,"singleTask" 和 "singleInstance" 启动模式的Activity组件被限制只能存在一个唯一的实例。这是因为这样的实例在自身独立的task堆栈中是根Activity的角色,在一个task中只能存在一个这样的组件实例。
*
三、Whether the instance can have other activities in its task ---该Activity组件实例是否允许它所在的task堆栈中有其他Activity组件实例?. A "singleInstance" activity stands alone as the only activity in its task. If it starts another activity, that activity will be launched into a different task regardless of its launch mode — as if FLAG_ACTIVITY_NEW_TASK was in the intent. In all other respects, the "singleInstance" mode is identical to "singleTask".
翻译:"singleInstance" 启动模式的activity是不允许所处的task中出现其他activity实例的。这就意味着如果说这个activity激活其他activity的时候,被激活的activity是不可能与它同处于一个task堆栈的,被激活的activity只能是归属另外一个新的task堆栈的。这就如同这个 activity所发送的intent对象中的flag属性被设定为FLAG_ACTIVITY_NEW_TASK一样。除了这一点以外,singleInstance" 启动模式 和"singleTask"启动模式是一样的。
The other three modes permit multiple activities to belong to the task. A "singleTask" activity will always be the root activity of the task, but it can start other activities that will be assigned to its task. Instances of "standard" and "singleTop" activities can appear anywhere in a stack.
翻译:其他三个启动属性值的Activity组件实例是允许自己所处的task有多个Activity实例存在的。"singleTask" 启动模式的activity实例在堆栈中是根元素的角色,它可以激活其他 activity并压入到自己所处的task堆栈中。"standard" and "singleTop"启动模式的activity实例可以出现在一个task堆栈中的任意位置。
* 四、Whether a new instance of the class will be launched to handle a new intent ----是否可以再创建一个这样的activity实例来响应intent对象?. For the default "standard" mode, a new instance is created to respond to every new intent. Each instance handles just one intent. For the "singleTop" mode, an existing instance of the class is re-used to handle a new intent if it resides at the top of the activity stack of the target task. If it does not reside at the top, it is not re-used. Instead, a new instance is created for the new intent and pushed on the stack.
翻译:每当接收到一个intent对象的时候,启动模式是默认值"standard"的activity都是新创建一个实例来响应的,启动模式是"singleTop"的activity需要先判断当前task堆栈中的栈顶activity实例的类型是否和它一样,如果一样的话,直接使用栈顶实例来响应,如果不一样的话就创建新创建一个实例来响应,并且把这个新实例压入当前task堆栈。
For example, suppose a task's activity stack consists of root activity A with activities B, C, and D on top in that order, so the stack is A-B-C-D. 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 is expected to handle the new intent (since it's at the top of the stack) and the stack remains A-B-C-D.
翻译:例如。一个task堆栈中 A是根activity实例,还有B、C、D 三个activity实例,这时候activity D处于栈顶,那么这个堆栈的实例顺序是A-B-C-D。如果这时堆栈顶activity D需要激活一个activity D类型的activity实例,如果activity D的启动模式是"standard"的话,那么Android系统将新建一个这样的实例并且压入当前task堆栈,这时堆栈中的实例顺序将变为A-B-C-D-D。如果activity D的启动模式是"singleTop"的话,那么Android系统直接使用堆顶的这个activity D来应答intent,这时堆栈中的实例顺序依然保持为A-B-C-D。
If, on the other hand, the arriving intent is for an activity of type B, a new instance of B would be launched no matter whether B's mode is "standard" or "singleTop" (since B is not at the top of the stack), so the resulting stack would be A-B-C-D-B.
翻译:如果说intent要激活的是activity B类型的实例,那么Android系统将马上新建一个activity B实例,这时堆栈中的实例顺序将变为A-B-C-D-B。这是由于当前堆栈顶实例不是B,此时根本不会考虑activity B的启动模式是"standard" 还是 "singleTop" 。
As noted above, there's never more than one instance of a "singleTask" or "singleInstance" activity, so that instance is expected to handle all new intents. A "singleInstance" activity is always at the top of the stack (since it is the only activity in the task), so it is always in position to handle the intent. However, a "singleTask" activity may or may not have other activities above it in the stack. If it does, it is not in position to handle the intent, and the intent is dropped. (Even though the intent is dropped, its arrival would have caused the task to come to the foreground, where it would remain.)
翻译:前面我们已经说过:"singleTask" 或 "singleInstance" 启动模式的activity在一个task堆栈中是绝对不可能有多个同类实例的,任何对这样的Activity发出请求的相应都是由这个唯一实例来响应的,"singleInstance" 启动模式的activity总是在task堆栈的顶端,因为它是堆栈中唯一的实例,所以它同时也是根Activity的角色。但是,"singleTask" 启动模式的activity在堆栈中是可以有其他非同类的实例的,如果说这时候有一个intent试图再次激活一个"singleTask" 启动模式的activity,假如这时候恰好堆栈顶实例就是所需要的实例那么由该实例直接响应,如果不是,那么该intent请求失效,虽然失效但是会导致该task来到系统前台。
When an existing activity is asked to handle a new intent, the Intent object is passed to the activity in an onNewIntent() call. (The intent object that originally started the activity can be retrieved by calling getIntent().)
Note that when a new instance of an Activity is created to handle a new intent, the user can always press the BACK key to return to the previous state (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 what that instance was doing before the new intent arrived.
需要注意的是:如果是一个新的Activity实例来响应intent请求,用户按下BACK键的时候,界面回到前一个Activity界面,如果Android系统式用当前task堆栈中的现有实例来响应的话,用户即使按下BACK键,也不能回到前一个界面。
For more on launch modes, see the description of the <activity> element.