android:launchMode概述

android:launchMode
An instruction on how the activity should be launched. There are four modes that work in conjunction with activity flags (FLAG_ACTIVITY_* constants) in Intent objects to determine what should happen when the activity is called upon to handle an intent. They are:
"standard"
"singleTop"
"singleTask"
"singleInstance"
The default mode is "standard".

Activity有四种启动模式,默认为标准型。还是先给出表格,看的更加清楚。

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 the target 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 a new 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 system routes the intent to existing instance through a call to its onNewIntent() method, rather than creating a new one.
"singleInstance" No 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.

"standard"可以有多个相同实例,并且每次启动一个Activity,系统总是在任务栈中创建一个Activity新的实例并且为它设置意图的路径。

"singleTop"的多个实例是有条件的,如果一个Activity的实例已经存在与任务栈顶端,系统通过调用它的onNewIntent()函数来为意图设置到此实例的路径而非创建一个新的相同实例。

以上两个是常用的。

后两个不常用。

"singleTask"只有一个实例。在当前任务栈中,如果实例不存在,则会创建一个,如果这个Activity的实例已经存在,系统通过调用它的onNewIntent()函数来为意图设置到此实例的路径而非创建一个新的实例,即会结束它上面全部的Activity。如果另一个程序启动了这个Activity,并且此Activity已经在当前任务栈中创建,则不会在那个任务栈创建实例,而是使用引用指向当前任务栈中的实例。

例如:

B为singleTask模式

操作:A->B          A->B->C          A->B->C->B            A->B->C->B->C->A             A->B->C->B->C->A->B
实际:A->B          A->B->C          A->B                              A->B->C->A                               A->B

"singleInstance"Activity总是独占一个任务栈。如果A中启动B,系统为B另外开辟一个任务栈。

上一篇:python全栈 字符串,整数,bool 数据类型运用


下一篇:JAVA中的String类(详解)