標籤:
android:launchModeAn 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".
Activity有四種啟動模式,預設為標準型。還是先給出表格,看的更加清楚。
| 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"可以有多個相同執行個體,並且每次啟動一個Activity,系統總是在任務棧中建立一個Activity新的執行個體並且為它設定意圖的路徑。
"singleTop"的多個執行個體是有條件的,如果一個Activity的執行個體已經存在與任務棧頂端,系統通過調用它的onNewIntent()函數來為意圖設定到此執行個體的路徑而非建立一個新的相同執行個體。
以上兩個是常用的。
後兩個不常用。
"singleTask"只有一個執行個體。在當前任務棧中,如果執行個體不存在,則會建立一個,如果這個Activity的執行個體已經存在,系統通過調用它的onNewIntent()函數來為意圖設定到此執行個體的路徑而非建立一個新的執行個體,即會結束它上面全部的Activity。如果另一個程式啟動了這個Activity,並且此Activity已經在當前任務棧中建立,則不會在那個任務棧建立執行個體,而是使用引用指向當前任務棧中的執行個體。
例如:
B為singleTask模式
操作:A->B A->B->C A->B->C->B A->B->C->B->C->A A->B->C->B->C->A->B
實際:A->B A->B->C A->B A->B->C->A A->B
"singleInstance"Activity總是獨佔一個任務棧。如果A中啟動B,系統為B另外開闢一個任務棧。
android:launchMode概述