Android:Launch Mode:[enum]

来源:互联网 发布:支持向量回归算法 编辑:程序博客网 时间:2024/05/21 11:24
设置activity的启动模式。Activity的启动模式一共有四种。"standard", "single Top", "single Task", "single Instance"。四种模式可以大概的分成两组,”standard”和”single Top”是一组,其余的两个是一组。”standard”和”single Top”可以有多个activity的实例,而后面的两个,只能存在一个实例。

Single Task和Single Instance的Activity在每次启动的时候都会创建一个新的Task,而这个Single Task和Single Instance的Activity会是这个新建的Task的根Activity(Single Task和Single Instance只能作为某一个Task的根Activity)。这样的Activity只存在一个实例,也只存在一个这样的Task。

Single Task和Single Instance只有一点区别。Single Task的Activity允许其它的Activity加入到它的这个Task中来,而它始终是这个Task的根Activity。而Single Instance的Activity不允许其它的Activity加入到它的这个Task中来,如果Single Instance的Activity启动了另外的一个Activity,那么这个新的Activity会被加入到一个另外的Task中(或者创建一个新的Task),就好像在Intent里加入了一个FLAG_ACTIVITY_NEW_TASK的flag。需要注意的是Single Task,虽然Single Task的Activity允许其它的Activity加入到它的Task,但是,按HOME键返回后,再次从Launcher启动这个Single Task的Activity时,它还会去尝试创建一个新的Task,而这时它会发现已经有一个以这个Activity作为根Activity的Task存在,因此它会把这个已有的Task清空,除了这个根Activity之外。

Standard和Single Top也只有一点区别。Standard每次都会创建一个新的Activity实例来处理这个Intent,而Single Top在大多数的时候也会创建一个新的Activity实例,只有当目标Task的堆栈的栈顶已经有一个这个Activity的实例的时候不会再创建新的实例。而是由栈顶的实例的onNewIntent()来接受这个Intent。

英文说明:

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

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 mode can be instantiated multiple times. The instances can belong to any task and can be located anywhere in the activity stack. Typically, they're launched into the task that called startActivity() (unless the Intent object contains a FLAG_ACTIVITY_NEW_TASK instruction, in which case a different task is chosen — see the taskAffinity attribute).

In contrast, "singleTask" and "singleInstance" activities can 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 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 be created to handle a new intent. However, if the target task already has an existing instance of the activity at the top of its stack, that instance will receive the new intent (in an onNewIntent() 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 of the 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 from each other in only one respect: A "singleTask" activity allows other activities to be part of its task. It's at the root of the activity stack, but other activities (necessarily "standard" and "singleTop" activities) can be launched into the same 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.

For more information on launch modes and their interaction with Intent flags, see the Activities and Tasks section of the Application Fundamentals document.
0 0
原创粉丝点击