您的位置:首页 > 其它

Activity四种启动模式

2016-09-28 19:23 288 查看
android:launchMode
       启动模式
An instruction onhow the activity should be launched. There are four modes that work inconjunction with activity flags (FLAG_ACTIVITY_* constants) in

Intent objects to determine what should happenwhen the activity is called upon to handle an intent. They are:
activity如何被启动的一个指导。有四种模式结合在Intent对象的activity标记(FLAG_ACTIVITY_* constants) 共同决定当activity被调用时根据什么处理一个intent。他们是:
"standard"

"singleTop"

"singleTask"

"singleInstance"
The default mode is "standard".
默认模式是"standard"。
As shown in the table below, the modes
fall into twomain groups, with "standard" and "singleTop" activities onone side, and "singleTask" and "singleInstance" activitieson the other. An activity with the "standard" or"singleTop" launch mode can be instantiated
multiple times. Theinstances can belong to any task and can be located anywhere in the activitystack. Typically, they're launched into the task that called

startActivity() (unless the Intent objectcontains a

FLAG_ACTIVITY_NEW_TASK instruction, in which casea different task is chosen — see the

taskAffinity attribute).
就像下边的表显示的那样,这些模式从属于两个主要的组,"standard" 和 "singleTop"在一边,"singleTask"和 "singleInstance"在另外一边。一个使用"standard" 或 "singleTop"启动模式的activity能被实例化多次。这个实例能从属于任何任务,并且能被放置在activity栈的任何地方。典型的,他们被启动在任务里通过调用startActivity()函数(除非那个Intent对象包含FLAG_ACTIVITY_NEW_TASK
指示 ,在这样的case下,一个不同的任务就会被选择 – 可以看 taskAffinity 属性)。
In contrast, "singleTask" and "singleInstance" activities canonly 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 suchtask.

相对的,"singleTask" 和"singleInstance" 只能开启一个新任务。他们总是activity栈的根。此外,设备只能拥有这个activity的一个实例,在一个时间 –只有这么一个任务。
The "standard" and "singleTop" modesdiffer from each other in just one
respect: Every timethere's a new intent for a "standard" activity, a new instance of theclass is created to respond to that intent. Each instance handles a singleintent. Similarly, a new instance of a "singleTop" activity
may alsobe created to handle a new intent. However, if the target task already has anexisting instance of the activity at the top of its stack, that instance willreceive the new intent (in an

onNewIntent() call); a new instance is notcreated. In other
circumstances — for example, if anexisting 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 inthe target task — a new instance would
be created and pushed on the stack.
"standard" 和"singleTop"模式不同于每个其他的模式在这个方面:每次有一个新的使用"standard"模式启动activity的intent,一个新的类实例就会被创建,用来响应这次intent。每个实例处理一个单独的intent。相似的,一个"singleTop"模式的activity新实例也会被创建用来处理一个新的intent。然而,如果目标任务里已经存在一个这个activity的实例在它的栈的栈顶,这个栈顶的实例将会接收这个新的intent(用 onNewIntent()
调用);一个新的实例不会被创建。在其他情况下– 例如,如果一个"singleTop"模式activity的实例已经存在于目标任务,但是不在栈顶,或者它在栈顶,但不在目标任务里 –一个新的实例需要被创建并且被压入栈。
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 theroot 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, thatactivity is assigned to a different task — as if FLAG_ACTIVITY_NEW_TASK was inthe intent.

"singleTask" 和 "singleInstance"模式也不同于每个其他模式在这个方面:一个"singleTask"模式activity允许其他activity成为它的任务的一部分。它总是它的任务的根,但是其他activity(必须是"standard" 和 "singleTop"的activity)能被启动进入这个任务。一个"singleInstance"模式activity,另一方面,不许其他activity成为它的任务的一部分。它是任务里的唯一的activity。如果他启动其他activity,那个activity就会被指派一个不同的任务
– 就像FLAG_ACTIVITY_NEW_TASK 在intent里那样。
Use Cases
Launch Mode
Multiple Instances?
Comments
Normal launches for most activities
多数activity正常启动方式
"standard"
Yes
Default. The system always creates a new instance of the activity in the target task and
routes
the intent to it.
默认模式。系统总是会创建一个新的activity实例在目标任务里,并且将intent路由到这个实例里。
"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.
如果一个activity的实例已经存在于目标人的栈顶,系统将会通过调用onNewInent()方法路由intent到这个实例,而不是创建一个新的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.
系统创建activity作为一个新任务的根,并且路由intent到这个activity。然而,如果这个activity的实例已经存在,系统就会
路由intent到这个已存在的实例,通过调用onNewIntent()方法,而不是创建一个新的。
"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.
很像"singleTask",除了系统不会启动其他activity到拥有这个实例的任务里。这个activity总是单个唯一的,对于它的任务来说。
As shown in the table above, standard is the default modeand is appropriate for most types of activities. SingleTop is also a common anduseful 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 verydifferent from most other applications.
就像上边表显示的那样,"standard"是默认模式,并且对大多数activity是适当的。"singleTop"对于大多数activity也是一个通用和有用的模式。其他模式— singleTask 和singleInstance —就不适用于多数app了,因为他们会导致一个交互模式,这个模式对用户来说很可能是不熟悉的,并且是非常不同于其他大多数app。
Regardless of the launch mode that you choose, make sureto test the
usability
of the activity duringlaunch and when navigating back to it from other activities and tasks using theBack button.

不管你选择哪个启动模式,确认在activity启动时测试可用性,并且从其他activity和任务导航到当前这个activity需要使用Back键。
For more information on launch modes and theirinteraction with Intent flags, see the

Tasks and Back Stack document.
更多关于启动模式和他们与Intent标记交互的信息,看Tasks and Back Stack 文档。
 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  activity 模式