深入Android系統【2】:launchMode (Instance)深入理解

來源:互聯網
上載者:User

前文Android Trick 4中已經對launchMode做了簡介,近來使用中發現了一些理解不夠的地方,特寫出來和大家交流。

standard、singleTop、singleTask、singleInstance。

看SDK文檔後,發現也不是很好理解上面幾個的區別,嘗試著綜合各方面資料加上寫程式實現來理解一下,整理出來。

分為兩組去看比較好理解,第一組就是standard和singleTop,

standard:標準方式,啟動一個Activity就建立一個執行個體。

在沒有申明任何方式的情況下,這就是預設的Activity啟動方式了。

singleTop:當Activity棧最頂層的A1啟動另一個A1時,不會啟動新執行個體。

這兩個是人類最自然能夠理解的東西了,如果某一個應用當中讓你想到要用以上兩個設定的話,多半是正確的;相反,如果一個應用中你想到的是用以下第二組中的兩個設定的話,多半可能是用錯的。這個要非常注意。

第二組就是singleTask和singleInstance,

singleTask:在Task範圍內只產生一個執行個體。且這個執行個體啟動完成後是在棧頂。

如果在一個apk應用裡,singleTask的A啟動了一個B,B中啟動了A,這樣的情況怎麼辦呢?如果A是在原Task中啟動,系統會將B結束,然後保持原A在棧頂。

設定了"singleTask"啟動模式的Activity的特點:

1. 設定了"singleTask"啟動模式的Activity,它在啟動的時候,會先在系統中尋找屬性值affinity等於它的屬性值taskAffinity的任務存在;如果存在這樣的任務,它就會在這個任務中啟動(此後在根據第2條檢查),否則就會在新任務中啟動。因此,如果我們想要設定了"singleTask"啟動模式的Activity在新的任務中啟動,就要為它設定一個獨立的taskAffinity屬性值。

2. 如果設定了"singleTask"啟動模式的Activity不是在新的任務中啟動時,它會在已有的任務中查看是否已經存在相應的Activity執行個體,如果存在,就會把位於這個Activity執行個體上面的Activity全部結束掉,即最終這個Activity執行個體會位於任務的Stack頂端中(官方文檔說同時也在root of Stack,Stack底端,存疑1)。

singleInstance:launch一個singleInstance的Activity A會單獨開一個Task 1,並且在此Task中僅有此Activity,也就是說在此Activity中再啟動一個Activity B,這個B會在一個新的Task中開啟。在B中launch一個A的話,Task 1就會到前面來,並且restart Activity A。由此可能,在全域範圍內只有一個,即使在不同apk中調用(此處存疑2,有可能是不成立的)。它在Task中的上面下面都不會有其他Activity壓著或被壓著。

官方文檔給的解釋是這樣的:

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 called
startActivity()(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 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 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.

The "singleTask" and "singleInstance" modes also differ fromeach 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 thetarget 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 anew 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 systemroutes the intent to existing instance through a call to its
onNewIntent() method, rather than creating anew one.
"singleInstance" No Same 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 and singleInstance — 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 the BACK key.

For more information on launch modes and their interaction with Intentflags, see the

Tasks and Back Stackdocument.

"singleTask"
The system creates a new task and instantiates the activity at the root of the new task.However, if an instance of the activity already exists in a separate task, the system routes the intent to the existing instance through a call to its
onNewIntent() method, rather than creating a new instance. Only one instance of the activity can exist at a time.

Note: Although the activity starts in a new task, theBACK key still returns the user to the previous activity.

"singleInstance".
Same as "singleTask", except that the system doesn't launch any other activities intothe task holding the instance. The activity is always the single and only member of its task;any activities started by this one open in a separate ta

android:taskAffinity

The task that the activity has an affinity for. Activities with the same affinity conceptually belong to the same task (to the same"application" from the user's perspective). The affinity of a task is determined by the affinity of its root
activity.

The affinity determines two things — the task that the activity is re-parented to (see the
allowTaskReparenting attribute) and the task that will house the activity when it is launched with the
FLAG_ACTIVITY_NEW_TASK flag.

By default, all activities in an application have the same affinity. Youcan set this attribute to group them differently, and even placeactivities defined in different applications within the same task. To specify that the activity does not have an affinity
for any task, setit to an empty string.

If this attribute is not set, the activity inherits the affinity set for the application (see the
<application> element's
taskAffinityattribute). The name of the default affinity for an application is the package name set by the
<manifest> element.

所以,官方文檔中可能沒說清的疑點(產生矛盾了)有兩個: (1) 相同taskAffinity屬性值情況下,SingleTask Activity啟動後並一定在root of the task stack,那就不符合always root of stack了,矛盾。  (2) 官方文檔中明確說了SingleTask/Instance在Task範圍內僅有一個,並暗示了全域範圍內只有一個,即使在不同apk中調用。taskAffinity不同的情況下新開Task來承載Activity的話又怎麼解釋呢。既全域範圍記憶體在多個SingeInstance的Activity的執行個體一定不會發生嗎。只能說肯定的是同一taskAffinity值的SingleTask/Intance全域範圍內僅存在一個。

這兩個疑問暫時擱置,等之後我花點實際仔細研讀相關framework代碼後把它弄個明白吧。以後寫一篇Back Stack\ Affinity\ App Model\ launchMode相結合理解的文章。

總體來說,從開發實踐上來說,singleTop和standard比較好理解,行為比較符合一般邏輯。但牽涉到SingleTask/Instance的Activity,請實際測實驗證之後來決定有無效果。

參考文檔:

http://www.linuxidc.com/Linux/2011-08/41837p6.htm

http://developer.android.com/guide/topics/manifest/activity-element.html

http://groups.google.com/group/android-developers/browse_thread/thread/a24346d44a57fe5b?pli=1

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.