LaunchMode
1. standard
- 系统默认模式,每次启动activity都会重新创建一个新的实例,在这种模式下谁启动了standard的activity,那么这个activity就运行在启动他的那个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
- 栈顶复用模式,如果新activity已经位于任务栈的栈顶,那么此activity不会被重新创建,同时他的onNewIntent方法会被回调。
- 应用场景,从通知启动的activity,例如新闻内容页面,每次收到的新闻推送通知,都跳转到相同的界面。
1>2,1是standard,2是singleTop,与standard相同
2->2
2已经位于栈顶没有被重新创建
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}
对比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
- 栈内复用模式,如果栈中存在此activity,那么将其调到栈顶,并且将其上的activity出栈。
- 应用场景,主界面,不管我们打开多少个activity,只要我们再回到主界面,其按返回键都应该退出应用,比如在微博浏览一堆页面会,回到主页,应该保证其上的activity都被移除,而不是在栈顶加一个新的主页实例,这样返回时不能退出应用,反而回到之前的页面。
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启动2后,1和2分别位于栈A,栈B,2再启动3,3在栈B,3再启动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
- 单实例模式,系统唯一,单独位于一个栈中。
- 来电呼叫界面,系统全局唯一的。还有就是分享界面,这种不需要跟别的界面在一个任务中的情况,如微信分享界面,在相册中选择一张图片点击分享到微信,这个微信分享选择联系人界面就是singleInstance,选择联系人后会开启对话界面,然后在这个界面后退,会回到主界面而不是分享选择联系人界面。
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}
下面这种情况对比singleTask
1->2->3->2
1 standard,2 singleInstance,3 standard
1启动2后,1和2分别位于栈A,栈B,2再启动3,3在新建的栈C中,3再启动2,找到栈B中的2,此时栈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区别:singleInstance 独占一个栈,singleTask在栈中找到就置顶,并且其上activity出栈,可以和TaskAffinity联合使用,指定所在任务栈。
网友评论