LaunchMode
1. standard
- 系統(tǒng)默認(rèn)模式方妖,每次啟動(dòng)activity都會(huì)重新創(chuàng)建一個(gè)新的實(shí)例,在這種模式下誰(shuí)啟動(dòng)了standard的activity须喂,那么這個(gè)activity就運(yùn)行在啟動(dòng)他的那個(gè)activity所在棧吁断。
1:com.example.lzh_d.launchmodetest/.MainActivity
2:com.example.lzh_d.launchermodetest2/.MainActivity
3:com.example.lzh_d.launchermodetest2/.SecondActivity
1->2
1.2都是standard
Running activities (most recent first):
TaskRecord{af6f390 #3512 A=com.example.lzh_d.launchmodetest U=0 StackId=1 sz=2}
Run #10: ActivityRecord{8ffb8bc u0 com.example.lzh_d.launchermodetest2/.MainActivity t3512}
Run #9: ActivityRecord{6107fde u0 com.example.lzh_d.launchmodetest/.MainActivity t3512}
TaskRecord{f5a8266 #3491 A=com.tencent.mm U=0 StackId=1 sz=1}
Run #8: ActivityRecord{7a1cfe4 u0 com.tencent.mm/.ui.LauncherUI t3491}
TaskRecord{fff18d2 #3509 A=com.example.lzh_d.launchermodetest2 U=0 StackId=1 sz=1}
Run #7: ActivityRecord{2abdc4f u0 com.example.lzh_d.launchermodetest2/.MainActivity t3509}
2. singleTop
- 棧頂復(fù)用模式,如果新activity已經(jīng)位于任務(wù)棧的棧頂坞生,那么此activity不會(huì)被重新創(chuàng)建仔役,同時(shí)他的onNewIntent方法會(huì)被回調(diào)。
- 應(yīng)用場(chǎng)景是己,從通知啟動(dòng)的activity又兵,例如新聞內(nèi)容頁(yè)面,每次收到的新聞推送通知卒废,都跳轉(zhuǎn)到相同的界面沛厨。
1>2,1是standard摔认,2是singleTop,與standard相同
2->2
2已經(jīng)位于棧頂沒(méi)有被重新創(chuàng)建
Running activities (most recent first):
TaskRecord{ba16c5d #3522 A=com.example.lzh_d.launchermodetest2 U=0 StackId=1 sz=1}
Run #2: ActivityRecord{e7c20de u0 com.example.lzh_d.launchermodetest2/.MainActivity t3522}
TaskRecord{f3d9ce8 #3341 A=com.netease.newsreader.activity U=0 StackId=1 sz=1}
Run #1: ActivityRecord{893c1a9 u0 com.netease.newsreader.activity/com.netease.nr.phone.main.MainActivity t3341}
TaskRecord{8a730f7 #3180 A=com.android.incallui U=0 StackId=1 sz=1}
Run #0: ActivityRecord{b389462 u0 com.android.incallui/.InCallScreen t3180}
對(duì)比standard
Running activities (most recent first):
TaskRecord{8249d5b #3520 A=com.example.lzh_d.launchermodetest2 U=0 StackId=1 sz=2}
Run #2: ActivityRecord{2b9ad91 u0 com.example.lzh_d.launchermodetest2/.MainActivity t3520}
Run #1: ActivityRecord{f40170f u0 com.example.lzh_d.launchermodetest2/.MainActivity t3520}
TaskRecord{8a730f7 #3180 A=com.android.incallui U=0 StackId=1 sz=1}
Run #0: ActivityRecord{b389462 u0 com.android.incallui/.InCallScreen t3180}
3. singleTask
- 棧內(nèi)復(fù)用模式逆皮,如果棧中存在此activity,那么將其調(diào)到棧頂参袱,并且將其上的activity出棧电谣。
- 應(yīng)用場(chǎng)景秽梅,主界面,不管我們打開(kāi)多少個(gè)activity剿牺,只要我們?cè)倩氐街鹘缑嫫罂眩浒捶祷劓I都應(yīng)該退出應(yīng)用,比如在微博瀏覽一堆頁(yè)面會(huì)晒来,回到主頁(yè)钞诡,應(yīng)該保證其上的activity都被移除,而不是在棧頂加一個(gè)新的主頁(yè)實(shí)例湃崩,這樣返回時(shí)不能退出應(yīng)用荧降,反而回到之前的頁(yè)面。
1->2
1 standard,2 singleTask
Running activities (most recent first):
TaskRecord{c94e00b #3513 A=com.example.lzh_d.launchermodetest2 U=0 StackId=1 sz=1}
Run #9: ActivityRecord{ffe3677 u0 com.example.lzh_d.launchermodetest2/.MainActivity t3513}
TaskRecord{af6f390 #3512 A=com.example.lzh_d.launchmodetest U=0 StackId=1 sz=1}
Run #8: ActivityRecord{6107fde u0 com.example.lzh_d.launchmodetest/.MainActivity t3512}
TaskRecord{f5a8266 #3491 A=com.tencent.mm U=0 StackId=1 sz=1}
Run #7: ActivityRecord{7a1cfe4 u0 com.tencent.mm/.ui.LauncherUI t3491}
1->2->3->2
1 standard,2 singleTask,3 standard
1啟動(dòng)2后攒读,1和2分別位于棧A誊抛,棧B,2再啟動(dòng)3,3在棧B,3再啟動(dòng)2,3出棧,棧B中只有2.
Running activities (most recent first):
TaskRecord{c5fb4e7 #3535 A=com.example.lzh_d.launchermodetest2 U=0 StackId=1 sz=1}
Run #3: ActivityRecord{c101916 u0 com.example.lzh_d.launchermodetest2/.MainActivity t3535}
TaskRecord{8ea71d7 #3525 A=com.example.lzh_d.launchmodetest U=0 StackId=1 sz=1}
Run #2: ActivityRecord{3ecef32 u0 com.example.lzh_d.launchmodetest/.MainActivity t3525}
TaskRecord{f3d9ce8 #3341 A=com.netease.newsreader.activity U=0 StackId=1 sz=1}
Run #1: ActivityRecord{893c1a9 u0 com.netease.newsreader.activity/com.netease.nr.phone.main.MainActivity t3341}
TaskRecord{8a730f7 #3180 A=com.android.incallui U=0 StackId=1 sz=1}
Run #0: ActivityRecord{b389462 u0 com.android.incallui/.InCallScreen t3180}
4. singleInstance
- 單實(shí)例模式整陌,系統(tǒng)唯一,單獨(dú)位于一個(gè)棧中瞎领。
- 來(lái)電呼叫界面泌辫,系統(tǒng)全局唯一的。還有就是分享界面九默,這種不需要跟別的界面在一個(gè)任務(wù)中的情況震放,如微信分享界面,在相冊(cè)中選擇一張圖片點(diǎn)擊分享到微信驼修,這個(gè)微信分享選擇聯(lián)系人界面就是singleInstance殿遂,選擇聯(lián)系人后會(huì)開(kāi)啟對(duì)話界面,然后在這個(gè)界面后退乙各,會(huì)回到主界面而不是分享選擇聯(lián)系人界面墨礁。
1->2
這種情況和singleTask一樣
Running activities (most recent first):
TaskRecord{20c273e #3524 A=com.example.lzh_d.launchermodetest2 U=0 StackId=1 sz=1}
Run #3: ActivityRecord{f624d33 u0 com.example.lzh_d.launchermodetest2/.MainActivity t3524}
TaskRecord{8ea71d7 #3525 A=com.example.lzh_d.launchmodetest U=0 StackId=1 sz=1}
Run #2: ActivityRecord{3ecef32 u0 com.example.lzh_d.launchmodetest/.MainActivity t3525}
TaskRecord{f3d9ce8 #3341 A=com.netease.newsreader.activity U=0 StackId=1 sz=1}
Run #1: ActivityRecord{893c1a9 u0 com.netease.newsreader.activity/com.netease.nr.phone.main.MainActivity t3341}
TaskRecord{8a730f7 #3180 A=com.android.incallui U=0 StackId=1 sz=1}
Run #0: ActivityRecord{b389462 u0 com.android.incallui/.InCallScreen t3180}
下面這種情況對(duì)比singleTask
1->2->3->2
1 standard,2 singleInstance,3 standard
1啟動(dòng)2后,1和2分別位于棧A耳峦,棧B,2再啟動(dòng)3,3在新建的棧C中,3再啟動(dòng)2,找到棧B中的2恩静,此時(shí)棧A,B蹲坷,C分別放著activity 1 驶乾,2 ,3.
Running activities (most recent first):
TaskRecord{2e79031 #3532 A=com.example.lzh_d.launchermodetest2 U=0 StackId=1 sz=1}
Run #4: ActivityRecord{9f8d08f u0 com.example.lzh_d.launchermodetest2/.MainActivity t3532}
TaskRecord{16d8316 #3533 A=com.example.lzh_d.launchermodetest2 U=0 StackId=1 sz=1}
Run #3: ActivityRecord{898ffc9 u0 com.example.lzh_d.launchermodetest2/.SecondActivity t3533}
TaskRecord{8ea71d7 #3525 A=com.example.lzh_d.launchmodetest U=0 StackId=1 sz=1}
Run #2: ActivityRecord{3ecef32 u0 com.example.lzh_d.launchmodetest/.MainActivity t3525}
TaskRecord{f3d9ce8 #3341 A=com.netease.newsreader.activity U=0 StackId=1 sz=1}
Run #1: ActivityRecord{893c1a9 u0 com.netease.newsreader.activity/com.netease.nr.phone.main.MainActivity t3341}
TaskRecord{8a730f7 #3180 A=com.android.incallui U=0 StackId=1 sz=1}
Run #0: ActivityRecord{b389462 u0 com.android.incallui/.InCallScreen t3180}
singleTask與singleInstance區(qū)別:singleInstance 獨(dú)占一個(gè)棧循签,singleTask在棧中找到就置頂级乐,并且其上activity出棧,可以和TaskAffinity聯(lián)合使用县匠,指定所在任務(wù)棧风科。