activity的四種啓動模式



當應用運行起來後就會開啓一條線程,線程中會運行一個任務棧,當Activity實例創建後就會放入任務棧中。Activity
啓動模式的設置在AndroidManifest.xml文件中,通過配置Activity的屬性android:launchMode=""設置,例如:

       <activity android:name=".MainActivity" android:launchMode="standard" />  

1. Standared模式(默認)
"standard" (the default mode)Default. The system creates a new instance of the activity in the task from which it was started and routes the intent to it. The activity can be instantiated multiple times, each instance can belong to different tasks, and one task can have multiple instances.
    默認標準的啓動模式, 每次startActivity都是創建一個新的activity的實例。適用於絕大大數情況

2. SingleTop模式

"singleTop"If an instance of the activity already exists at the top of the current 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. The activity can be instantiated multiple times, each instance can belong to different tasks, and one task can have multiple instances (but only if the activity at the top of the back stack is not an existing instance of the activity).

For example, suppose a task's back stack consists of root activity A with activities B, C, and D on top (the stack is A-B-C-D; D is on top). An intent arrives for an activity of type D. If D has the default "standard" launch mode, a new instance of the class is launched and the stack becomes A-B-C-D-D. However, if D's launch mode is "singleTop", the existing instance of D receives the intent through onNewIntent(), because it's at the top of the stack—the stack remains A-B-C-D. However, if an intent arrives for an activity of type B, then a new instance of B is added to the stack, even if its launch mode is "singleTop".

Note: When a new instance of an activity is created, the user can press the Back button to return to the previous activity. But when an existing instance of an activity handles a new intent, the user cannot press the Back button to return to the state of the activity before the new intent arrived in onNewIntent().

      單一頂部,如果要開啓的activity在任務棧的頂部已經存在,就不會創建新的實例,而是調用 onNewIntent() 方法。
      應用場景: 瀏覽器書籤。 避免棧頂的activity被重複的創建,解決用戶體驗問題。

Androidfest的配置:
<activity
    android:name="edu.fjnu.taskstack.SecondActivity"
    android:label="@string/_second"
    android:launchMode="singleTop">
</activity>

例如:
      若我有兩個Activity名爲B1,B2,兩個Activity內容功能完全相同,都有兩個按鈕可以跳到B1或者B2,唯一不同的是B1爲standard,B2爲singleTop。
      若我意圖打開的順序爲B1->B2->B2,則實際打開的順序爲B1->B2(後一次意圖打開B2,實際只調用了前一個的onNewIntent方法)
      若我意圖打開的順序爲B1->B2->B1->B2,則實際打開的順序與意圖的一致,爲B1->B2->B1->B2。

3. SingleTask模式

"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, the Back button still returns the user to the previous activity.

       單一任務棧 , activity只會在任務棧裏面存在一個實例。如果要激活的activity,在任務棧裏面已經存在,就不會創建新activity,而是複用這個已經存在的activity,調用 onNewIntent() 方法,並且清空當前activity任務棧上面所有的activity應用場景:瀏覽器activity, 整個任務棧只有一個實例,節約內存和cpu的目的
       注意: activity還是運行在當前應用程序的任務棧裏面的。不會創建新的任務棧。

Androidfest的配置:
<activity
    android:name="edu.fjnu.taskstack.SecondActivity"
    android:label="@string/_second"
    android:launchMode="singletask">
</activity>

例如:
 若我的應用程序中有三個Activity,C1,C2,C3,三個Activity可互相啓動,其中C2爲singleTask模式,那麼,無論我在這個程序中如何點擊啓動,如:C1->C2->C3->C2->C3->C1-C2,C1,C3可能存在多個實例,但是C2只會存在一個,並且這三個Activity都在同一個task裏面。
 但是C1->C2->C3->C2->C3->C1-C2,這樣的操作過程實際應該是如下這樣的,因爲singleTask會把task中在其之上的其它Activity destory掉。
 操作:C1->C2          C1->C2->C3          C1->C2->C3->C2            C1->C2->C3->C2->C3->C1             C1->C2->C3->C2->C3->C1-C2
實際:C1->C2          C1->C2->C3          C1->C2                              C1->C2->C3->C1                               C1->C2

4. SingleInstance模式

"singleInstance".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; any activities started by this one open in a separate task.

單一實例,整個手機操作系統裏面只有一個實例存在。不同的應用去打開這個activity共享 公用的同一個activity。他會運行在自己單獨,獨立的任務棧裏面,並且任務棧裏面只有他一個實例存在。

Androidfest的配置:
<activity
    android:name="edu.fjnu.taskstack.SecondActivity"
    android:label="@string/_second"
    android:launchMode="singleInstance">
</activity>
應用場景:呼叫來電界面 InCallScreen

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章