App Manifest:<activity>android:launchMode

An instruction on how the activity should be launched. There are four modesthat work in conjunction with activity flags ( FLAG_ACTIVITY_* constants)in Intent objects to determine what should happen whenthe activity is called upon to handle an intent. They are:

"standard"
"singleTop"
"singleTask"
"singleInstance"

The default mode is "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 modecan be instantiated multiple times. The instances can belong to any taskand can be located anywhere in the activity stack. Typically, they'relaunched into the task that calledstartActivity()(unless the Intent object contains aFLAG_ACTIVITY_NEW_TASKinstruction, in which case a different task is chosen — see thetaskAffinity attribute).

In contrast, "singleTask" and "singleInstance" activitiescan 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 otherin 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 becreated to handle a new intent. However, if the target task already has anexisting instance of the activity at the top of its stack, that instancewill receive the new intent (in anonNewIntent() 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 ofthe 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.

Similarly, if younavigateup to an activity on the current stack, the behavior is determined by theparent activity's launch mode. If the parent activity has launch modesingleTop (or theup intent containsFLAG_ACTIVITY_CLEAR_TOP), the parent is brought to thetop of the stack, and its state is preserved. The navigation intent is receivedby the parent activity'sonNewIntent()method. If the parent activity has launch modestandard (and theup intent does not containFLAG_ACTIVITY_CLEAR_TOP), the current activity and itsparent are both popped off the stack, and a new instance of the parent activityis created to receive the navigation intent.

The "singleTask" and "singleInstance" modes also differ fromeach other in only one respect: A "singleTask" activity allows otheractivities to be part of its task. It's always at the root of its task, butother 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 ifFLAG_ACTIVITY_NEW_TASK was in the intent.

Use CasesLaunch ModeMultiple Instances?Comments
Normal launches for most activities"standard"YesDefault. The system always creates a new instance of the activity in thetarget task and routes the intent to it.
"singleTop"ConditionallyIf 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 anew instance of the activity.
Specialized launches
(not recommended for general use)
"singleTask"NoThe system creates the activity at the root of a new task and routes theintent to it. However, if an instance of the activity already exists, the systemroutes the intent to existing instance through a call to itsonNewIntent() method, rather than creating anew one.
"singleInstance"NoSame as "singleTask", except that the system doesn't launch anyother activities into the task holding the instance. The activity is always thesingle and only member of its task.

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

Regardless of the launch mode that you choose, make sure to test the usabilityof the activity during launch and when navigating back to it fromother activities and tasks using theBack button.

For more information on launch modes and their interaction with Intentflags, see theTasks and Back Stackdocument. 


介绍关于activity的启动方式,他们分为4种方式当被启动的时候:

"standard"
"singleTop"
"singleTask"
"singleInstance"

默认是standard

当启动前,4中模式被分成两组,standard和singleTop,singleTask和singleInstance.standard和singleTop可以被实例化多次,这个实例可以属于任何task和可以位于任何stack中。典型的例子就是程序被startActivity触发。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值