Activity的属性之—launchMode
Activity的属性之—launchMode
成都创新互联公司专业提供雅安服务器托管服务,为用户提供五星数据中心、电信、双线接入解决方案,用户可自行在线购买雅安服务器托管服务,并享受7*24小时金牌售后服务。
在manifest清单文件中配置activity的时候,有一个属性叫launchMode
/> LaunchMode指定该activity的加载模式,如上所述加载模式有四种分别是 standard singleTopsingleTask 和 singleInstance . 为什么要指定activity的加载模式呢?因为Android系统对activity是通过栈来管理的,当应用启动的时候相应的系统就会为这个应用创建一个栈,然后启动应用的main activity。 就相当于数据结构中的出栈和入栈一样的管理本应用中的activity。而activity的加载模式就可以管理activity的实例化和加载方式,以及加载的顺序关系等。 那么下面来介绍一下这四种加载模式: 1、 standard 这个模式是activity默认启动模式,也就是说在不指定activity的launchMode的情况下就是按照这种模式启动的,这种模式下每次启动一个新的activity的时候都会为目标activity创建一个新的实例,并将此实例添加到栈中(也就是在栈顶)(PS一般的那个钱显示给用户的activity都在栈顶)。 2、 singleTop 在这个模式下的时候,在启动目标的activity的时候,如果当前应用的栈中的栈顶已经是目标activity的实例的话那么就不会重新创建新的实例,而直接使用栈顶的实例。(如果被启动目标没有在栈顶,那么此时系统会重新创建一个该activity的实例并将它加载到栈顶,这时它和standard模式完全一样) 3、 singleTask 这种模式下,如果当前栈中有将要启动activity的实例那么直接调用这个实例,也就是在同一个栈中只有一个实例。使用 singleTop模式可以很好地解决重复创建栈顶活动的问题,但是如果该activity并没有处于栈顶的位置,还是可能会创建多个activity实例的。那么有没有什么办法可以让某个activity在整个应用程序的上下文中只存在一个实例呢?这就要借助singleTask模式来实现了。当activity的启动模式指定为 singleTask,每次启动该activity时系统首先会在返回栈中检查是否存在该activity的实例,如果发现已经存在则直接使用该实例,并把在这个activity之上的所有activity统统出栈,如果没有发现就会创建一个新的activity实例 4、 singleInstance singleInstance模式应该算是四种启动模式中最特殊也最复杂的一个了,你也需要多花点功夫来理解这个模式。不同于以上三种启动模式,指定为 singleInstance模式的activity会启用一个新的返回栈来管理这个activity(其实如果singleTask模式指定了不同的 taskAffinity,也会启动一个新的返回栈)。那么这样做有什么意义呢?想象以下场景,假设我们的程序中有一个activity是允许其他程序调用的,如果我们想实现其他程序和我们的程序可以共享这个activity的实例,应该如何实现呢?使用前面三种启动模式肯定是做不到的,因为每个应用程序都会有自己的返回栈,同一个activity在不同的返回栈中入栈时必然是创建了新的实例。而使用singleInstance模式就可以解决这个问题,在这种模式下会有一个单独的返回栈来管理这个activity,不管是哪个应用程序来访问这个activity,都共用的同一个返回栈,也就解决了共享activity实例的问题。 顺便看下api文档的介绍片段: 摘自 http://developer.android.com/guide/topics/manifest/activity-element.html#lmode An instruction on how the activity should be launched. There are four modes
that work in conjunction with activity flags ( " The default mode is " As shown in the table below, the modes fall into two main groups, with
" In contrast, " The " 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
The " As shown in the table above, Regardless of the launch mode that you choose, make sure to test the
usability of the activity during launch and when navigating back to it from
other activities and tasks using the Back button. For more information on launch modes and their interaction with Intent flags,
see the Tasks and Back
Stack document 大概意思和我上面翻译的差不多, 欢迎指正 poarryScript@gmail.comandroid:launchMode=["multiple" |"singleTop" |
"singleTask" |"singleInstance"] android:launchMode
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
"standard
".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
(unless the Intent object contains a startActivity()
instruction, in which case a different task is chosen — see the taskAffinity attribute).FLAG_ACTIVITY_NEW_TASK
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.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.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 contain FLAG_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.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 if
FLAG_ACTIVITY_NEW_TASK
was in the 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 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
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.
网站标题:Activity的属性之—launchMode
文章来源:http://myzitong.com/article/pcceoh.html