Activity的启动模式分为以下几种:
Standard
SingleTop
SingleTask
SingleInstance
Android Developer 6.0的解释如下:
public static final int AndroidManifestActivity_launchMode
Specify how an activity should be launched. See the Tasks and Back Stack document for important information on how these options impact the behavior of your application.
If this attribute is not specified, standard launch mode will be used. Note that the particular launch behavior can be changed in some ways at runtime through the Intent flags FLAG_ACTIVITY_SINGLE_TOP, FLAG_ACTIVITY_NEW_TASK, and FLAG_ACTIVITY_MULTIPLE_TASK.
Must be one of the following constant values.
Constant Value Description
standard 0 The default mode, which will usually create a new instance of the activity when it is started, though this behavior may change with the introduction of other options such as Intent.FLAG_ACTIVITY_NEW_TASK.
singleTop 1 If, when starting the activity, there is already an instance of the same activity class in the foreground that is interacting with the user, then re-use that instance. This existing instance will receive a call to Activity.onNewIntent() with the new Intent that is being started.
singleTask 2 If, when starting the activity, there is already a task running that starts with this activity, then instead of starting a new instance the current task is brought to the front. The existing instance will receive a call to Activity.onNewIntent() with the new Intent that is being started, and with the Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT flag set. This is a superset of the singleTop mode, where if there is already an instance of the activity being started at the top of the stack, it will receive the Intent as described there (without the FLAG_ACTIVITY_BROUGHT_TO_FRONT flag set). See the Tasks and Back Stack document for more details about tasks.
singleInstance 3 Only allow one instance of this activity to ever be running. This activity gets a unique task with only itself running in it; if it is ever launched again with the same Intent, then that task will be brought forward and its Activity.onNewIntent() method called. If this activity tries to start a new activity, that new activity will be launched in a separate task. See the Tasks and Back Stack document for more details about tasks.
This corresponds to the global attribute resource symbol launchMode.
Constant Value: 14 (0x0000000e)
翻译:
说明一个activity是以何种模式加载呈现的,查看Tasks 和Back Stack文档,解释了这些设置项是怎样影响你的应用的呈现的。
如果这个参数没有被指定定义,那么默认使用standard模式;注意实际activity的启动模式可以在程序运行时通过Intent 的flags来改变,flags有FLAG_ACTIVITY_SINGLE_TOP、FLAG_ACTIVITY_NEW_TASK和FLAG_ACTIVITY_MULTIPLE_TASK。
必须是以下常量数据的一个:
Constant Value Description
standard 0 默认模式,activity启动的时候,会实例化一个新的activity的实例,然后
可以通过设置Intent.FLAG_ACTIVITY_NEW_TASK来改变启动形态
SingleTop 1 如果,当启动该activity时,处于最前面并与用户交互的地方已经存在了一
个相同的activity,那么,重新使用该实例(即不会重新实例化一个新的
activity实例),这个已经存在的实例当要启动的时候,会接受一个新的Intent
携带着Activity.onNewIntent();
SingleTask 2 如果,当启动该activity时,已经运行了一个任务栈用于启动该activity,
那么将当前的task“移到”最前面,而不是重新实例化一个。这个已经存在的实例当
要启动的时候,会接受一个新的Intent携带着Activity.onNewIntent();
设置了flag=Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT;这就存在一个
不同于SingleTop的扩充,如果在栈顶已经存在了将要启动的activity的一个实
例,它会接受一个如下描述的一个Intent(没有设置
FLAG_ACTIVITY_BROUGHT_TOFRONT),查看Tasks and Back Stack文档来
了解关于tasks的更多详情。
SingleInstance 3 仅允许该activity的一个实例运行(之前或现在),该activity拥有一个唯
一的task来运行它,当它被一个相同的intent启动的时候,这个task会被移到最
前面并且Activity.onNewIntent()方法将会被调用,如果在activity中启动
另外一个新的activity,则这个新的activity将会在一个另外一个task栈中被
实例化。查看Tasks and Back Stack文档可以了解关于tasks的更多详情。
自己的理解如下,如有不同见解,可留言一起讨论:
首先视图包含了一个activity栈和activity的实例:
默认是Standard模式:启动一个activity就会新创建一个activity的实例,然后将该实例添加到该activity栈的栈顶;
SingleTop:与Standard的区别是:当启动一个activity时,若activity栈顶就是该activity的一个实例,则不会重新实例化该activity的实例。
SingleTask:当启动activity时,若其他的activity栈中拥有该activity的实例,则将该栈移动到当前处于用户交互的activity栈中;若没有则新启动一个activity栈,然后在该处新建一个activity的实例
SingleInstance:同SingleTask,区别是若其他的activity栈中拥有该activity的实例,则只将该栈中此activity的实例移动到当前处于用户交互的activity栈中。