android:launchMode--四种启动模式

下面来看看官方文档是怎么说的:改天翻译

有四种模式:standard, singleTop, singleTask, singleInstance。默认模式是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 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_TASKinstruction, in which case a different task is chosen — see the taskAffinity attribute).

这四种模式分成了两组,standard和singleTop是一组,singleTask和singleInstance是另一组。第一组可以被实例化多次。这两个类型的实例可以属于任何一个任务,并且可以处于activity栈的任意位置。需特别注意的是,他们在那些调用了startActivity()的任务中被启动,除非Intent对象包含了这个指令FLAG_ACTIVITY_NEW_TASK,这样的话就一个不同的任务被选择。

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.

“singleTask”和“singleInstance”activity只能在一个task中。它们通常在activity的栈底。另外,设备一次只能hold该activity的一个实例--只在该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 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.

“standard”,“singleTop”只在一个方面不同:每次有一个standard activity在intent中new出来的时候,都会创建一个新的实例。一个实例handle一个单独的intent。“singleTop”activity 也可以创建出来handle一个新的intent。但是,如果目标任务已经有一个存在的该实例对象在栈顶,这个实例会接受新的intent,这时调用onNewIntent();不会创建新的实例。在另外的情况下,例如,已经有一个实例在target task,但是没在栈顶,或者在一个栈的栈顶,但是不是target task, 那么新的实例会被创建出来并push到栈顶。

Similarly, if you navigate up to an activity on the current stack, the behavior is determined by the parent activity's launch mode. If the parent activity has launch mode singleTop (or the up intent contains FLAG_ACTIVITY_CLEAR_TOP), the parent is brought to the top of the stack, and its state is preserved. The navigation intent is received by the parent activity's onNewIntent() method. If the parent activity has launch mode standard (and the up intent does not containFLAG_ACTIVITY_CLEAR_TOP), the current activity and its parent are both popped off the stack, and a new instance of the parent activity is created to receive the navigation intent.

如果在当前栈操纵activity,行为由父activity的启动方式决定。如果父activity启动方式是singleTop(或者启动的intent包含FLAG_ACTIVITY_CLIEAR_TOP),parent被带到栈顶,它的状态被保存。执行操作的intent收到父activity的onNewIntent()方法。如果parent activity是standard方式启动(intent没有FLAG_ACTIVITY_CLEAR_TOP),当前activity和它的parent都出栈,一个新的实例被创建出来去接收navigation intent。

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 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 ifFLAG_ACTIVITY_NEW_TASK was in the intent.

“singleTask”和“singleInstance”模式也在一个方面不同:“singleTask”activity允许其他activities成为它的task的一部分。它通常处于task的root,但是其它activities(必须是standard和singleTop)可以启动到这个task。一个singleInstance的activity,不允许其他activities成为它的task的一部分。它是它自己task中唯一的activity。如果它启动了另一个activity,那么那个activity被分配到另一个不同的task--就像FLAG_ACTIVITY_NEW_TASK在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 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 itsonNewIntent() 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.

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

standard是默认的,并且适合大多activities,singletop也是很常见有用的。singleTask和singleInstance不适用于大多应用,因为它们引起interaction model,对用户来说不熟悉,并且和大多应用不一样。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值