您好,登錄后才能下訂單哦!
Activity的屬性之—launchMode
在manifest清單文件中配置activity的時候,有一個屬性叫launchMode
<Activity ……
android:launchMode=["multiple" | "singleTop" |
"singleTask" | "singleInstance"]
/>
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
android:launchMode
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
".
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
(unless the Intent object contains a startActivity()
instruction, in which case a different task is chosen — see the taskAffinity attribute).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.
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.
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 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.
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 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. |
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.
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.com
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。