# 深入Android系统【2】：launchMode （Instance）深入理解

standard：标准方式，启动一个Activity就创建一个实例。

singleTop：当Activity栈最顶层的A1启动另一个A1时，不会启动新实例。

The default mode is "standard".

As shown in the table below, the modes fall into two main groups, with"standard" and "singleTop" activities on one side, and"singleTask" and "singleInstance" activities on the other.An activity with the "standard" or "singleTop" launch modecan be instantiated multiple times. The instances can belong to any taskand can be located anywhere in the activity stack. Typically, they'relaunched into the task that called startActivity()(unless the Intent object contains aFLAG_ACTIVITY_NEW_TASKinstruction, in which case a different task is chosen — see thetaskAffinity attribute).

In contrast, "singleTask" and "singleInstance" activitiescan only begin a task. They are always at the root of the activity stack.Moreover, the device can hold only one instance of the activity at a time— only one such task.

The "standard" and "singleTop" modes differ from each other in just one respect: Every time there's a new intent for a "standard"activity, a new instance of the class is created to respond to that intent.Each instance handles a single intent.Similarly, a new instance of a "singleTop" activity may also becreated to handle a new intent. However, if the target task already has anexisting instance of the activity at the top of its stack, that instancewill receive the new intent (in anonNewIntent() call);a new instance is not created.In other circumstances — for example, if an existing instance of the"singleTop" activity is in the target task, but not at the top ofthe stack, or if it's at the top of a stack, but not in the target task— a new instance would be created and pushed on the stack.

The "singleTask" and "singleInstance" modes also differ fromeach other in only one respect: A "singleTask" activity allows other activities to be part of its task. It's always at the root of its task, but other activities (necessarily "standard" and "singleTop"activities) can be launched into that task. A "singleInstance"activity, on the other hand, permits no other activities to be part of its task.It's the only activity in the task. If it starts another activity, that activity is assigned to a different task — as if FLAG_ACTIVITY_NEW_TASK was in the intent.

Use Cases Launch Mode Multiple Instances? Comments
Normal launches for most activities "standard" Yes Default. The system always creates a new instance of the activity in thetarget task and routes the intent to it.
"singleTop" Conditionally If an instance of the activity already exists at the top of the target task,the system routes the intent to that instance through a call to its onNewIntent() method, rather than creating anew instance of the activity.
Specialized launches
(not recommended for general use)
"singleTask" No The system creates the activity at the root of a new task and routes the intent to it. However, if an instance of the activity already exists, the systemroutes the intent to existing instance through a call to its onNewIntent() method, rather than creating anew one.
"singleInstance" No Same as "singleTask", except that the system doesn't launch anyother activities into the task holding the instance. The activity is always thesingle and only member of its task.

As shown in the table above, standard is the default mode and isappropriate for most types of activities. SingleTop is also acommon and useful launch mode for many types of activities. The other modes— singleTask and singleInstance — arenot appropriate for most applications,since they result in an interaction model that is likely to be unfamiliar tousers and is very different from most other applications.

Regardless of the launch mode that you choose, make sure to test the usabilityof the activity during launch and when navigating back to it fromother activities and tasks using the BACK key.

For more information on launch modes and their interaction with Intentflags, see the Tasks and Back Stackdocument.

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

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

"singleInstance".
Same as "singleTask", except that the system doesn't launch any other activities intothe 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 ta
android:taskAffinity
The task that the activity has an affinity for. Activities with the same affinity conceptually belong to the same task (to the same"application" from the user's perspective). The affinity of a task is determined by the affinity of its root activity.

The affinity determines two things — the task that the activity is re-parented to (see the allowTaskReparenting attribute) and the task that will house the activity when it is launched with the FLAG_ACTIVITY_NEW_TASK flag.

By default, all activities in an application have the same affinity. Youcan set this attribute to group them differently, and even placeactivities defined in different applications within the same task. To specify that the activity does not have an affinity for any task, setit to an empty string.

If this attribute is not set, the activity inherits the affinity set for the application (see the <application> element's taskAffinityattribute). The name of the default affinity for an application is the package name set by the <manifest> element.

http://www.linuxidc.com/Linux/2011-08/41837p6.htm

http://developer.android.com/guide/topics/manifest/activity-element.html