大家好,又见面了,我是你们的朋友全栈君。
目录
1. 概述
2. 详细过程
2.1 启动MainActivity
2.1.1 生命周期
2.1.2 Activity栈
2.2 MainActivity中启动SecondActivity
2.2.1 生命周期
2.2.2 Activity栈
2.3 SecondActivity中启动MainActivity
2.3.1 生命周期
2.3.2 Activity栈
2.4 MainActivity中启动SecondActivity
2.4.1 生命周期
2.4.2 Activity栈
3. 总结
4. Reference
1. 概述
总是看到Activity的四种启动模式:Standard,SingleTask,SingleTop,SingleInstance,而且都是借助于官方文档理解的。但是最近在写Demo测试这几种启动模式的特点与区别的时候,发现与官方文档有不一致的地方。本篇文章主要讲解的是SingleTask这种LaunchMode。
在Android官方文档中,对“singleTask”模式解释的是:The system creates a new task and instaniates the activity at the root of the new task。
但是在写demo测试的时候,发现这个说法是不准确的。这句话成立的条件是,要启动的Activity的affinity是唯一的,即当前所有的任务中没有与该affinity相同的任务。
Demo中包含两个Activity,Activity的LaunchMode:
代码语言:javascript复制MainActivity-Standard
SecondActivity- SingleTask
步骤:
- 启动MainActivity;
- MainActivity中启动SecondActivity
- SecondActivity中启动MainActivity
- MainActivity中启动SecondActivity
使用命令:“dumpsys activity activities“,dump Activity栈的信息。
2. 详细过程
2.1 启动MainActivity
2.1.1 生命周期
代码语言:javascript复制2019-02-25 08:05:36.334 3077-3077/com.maureen.testtwoactivity D/[APP_Maureen]MainActivity: onCreate:this=com.maureen.testtwoactivity.MainActivity@f6993c1
2019-02-25 08:05:36.528 3077-3077/com.maureen.testtwoactivity D/[APP_Maureen]MainActivity: onStart:this=com.maureen.testtwoactivity.MainActivity@f6993c1
2019-02-25 08:05:36.531 3077-3077/com.maureen.testtwoactivity D/[APP_Maureen]MainActivity: onResume:this=com.maureen.testtwoactivity.MainActivity@f6993c1
2.1.2 Activity栈
代码语言:javascript复制TaskRecord{832ddac #32 A=com.maureen.testtwoactivity U=0 StackId=1 sz=1}
Run #0: ActivityRecord{2de94c6 u0 com.maureen.testtwoactivity/.MainActivity t32}
2.2 MainActivity中启动SecondActivity
2.2.1 生命周期
代码语言:javascript复制2019-02-25 08:06:22.582 3077-3077/com.maureen.testtwoactivity D/[APP_Maureen]MainActivity: onPause:this=com.maureen.testtwoactivity.MainActivity@f6993c1
2019-02-25 08:06:22.669 3077-3077/com.maureen.testtwoactivity D/[APP_Maureen]SecondActivity: onCreate:this=com.maureen.testtwoactivity.SecondActivity@d5d6a26
2019-02-25 08:06:22.772 3077-3077/com.maureen.testtwoactivity D/[APP_Maureen]SecondActivity: onStart:this=com.maureen.testtwoactivity.SecondActivity@d5d6a26
2019-02-25 08:06:22.776 3077-3077/com.maureen.testtwoactivity D/[APP_Maureen]SecondActivity: onResume:this=com.maureen.testtwoactivity.SecondActivity@d5d6a26
2019-02-25 08:06:23.520 3077-3077/com.maureen.testtwoactivity D/[APP_Maureen]MainActivity: onStop:this=com.maureen.testtwoactivity.MainActivity@f6993c1
2019-02-25 08:06:23.522 3077-3077/com.maureen.testtwoactivity D/[APP_Maureen]MainActivity: onSaveInstanceState:this=com.maureen.testtwoactivity.MainActivity@f6993c1
2.2.2 Activity栈
代码语言:javascript复制TaskRecord{832ddac #32 A=com.maureen.testtwoactivity U=0 StackId=1 sz=2}
Run #1: ActivityRecord{8be2cd6 u0 com.maureen.testtwoactivity/.SecondActivity t32}
Run #0: ActivityRecord{2de94c6 u0 com.maureen.testtwoactivity/.MainActivity t32}
2.3 SecondActivity中启动MainActivity
2.3.1 生命周期
代码语言:javascript复制2019-02-25 08:07:46.098 3077-3077/com.maureen.testtwoactivity D/[APP_Maureen]SecondActivity: onPause:this=com.maureen.testtwoactivity.SecondActivity@d5d6a26
2019-02-25 08:07:46.165 3077-3077/com.maureen.testtwoactivity D/[APP_Maureen]MainActivity: onCreate:this=com.maureen.testtwoactivity.MainActivity@24103c8
2019-02-25 08:07:46.272 3077-3077/com.maureen.testtwoactivity D/[APP_Maureen]MainActivity: onStart:this=com.maureen.testtwoactivity.MainActivity@24103c8
2019-02-25 08:07:46.277 3077-3077/com.maureen.testtwoactivity D/[APP_Maureen]MainActivity: onResume:this=com.maureen.testtwoactivity.MainActivity@24103c8
2019-02-25 08:07:47.006 3077-3077/com.maureen.testtwoactivity D/[APP_Maureen]SecondActivity: onStop:this=com.maureen.testtwoactivity.SecondActivity@d5d6a26
2019-02-25 08:07:47.008 3077-3077/com.maureen.testtwoactivity D/[APP_Maureen]SecondActivity: onSaveInstanceState:this=com.maureen.testtwoactivity.SecondActivity@d5d6a26
2.3.2 Activity栈
代码语言:javascript复制TaskRecord{832ddac #32 A=com.maureen.testtwoactivity U=0 StackId=1 sz=3}
Run #2: ActivityRecord{3e6e847 u0 com.maureen.testtwoactivity/.MainActivity t32}
Run #1: ActivityRecord{8be2cd6 u0 com.maureen.testtwoactivity/.SecondActivity t32}
Run #0: ActivityRecord{2de94c6 u0 com.maureen.testtwoactivity/.MainActivity t32}
2.4 MainActivity中启动SecondActivity
2.4.1 生命周期
代码语言:javascript复制2019-02-25 08:08:51.142 3077-3077/com.maureen.testtwoactivity D/[APP_Maureen]MainActivity: onPause:this=com.maureen.testtwoactivity.MainActivity@24103c8
2019-02-25 08:08:51.174 3077-3077/com.maureen.testtwoactivity D/[APP_Maureen]SecondActivity: onNewIntent:this=com.maureen.testtwoactivity.SecondActivity@d5d6a26
2019-02-25 08:08:51.182 3077-3077/com.maureen.testtwoactivity D/[APP_Maureen]SecondActivity: onStart:this=com.maureen.testtwoactivity.SecondActivity@d5d6a26
2019-02-25 08:08:51.185 3077-3077/com.maureen.testtwoactivity D/[APP_Maureen]SecondActivity: onResume:this=com.maureen.testtwoactivity.SecondActivity@d5d6a26
2019-02-25 08:08:51.898 3077-3077/com.maureen.testtwoactivity D/[APP_Maureen]MainActivity: onStop:this=com.maureen.testtwoactivity.MainActivity@24103c8
2019-02-25 08:08:51.902 3077-3077/com.maureen.testtwoactivity D/[APP_Maureen]MainActivity: onDestroy:this=com.maureen.testtwoactivity.MainActivity@24103c8
2.4.2 Activity栈
代码语言:javascript复制TaskRecord{8ea7a0a #2 A=com.android.car.carlauncher U=0 StackId=0 sz=1}
Run #0: ActivityRecord{d99cab7 u0 com.android.car.carlauncher/.CarLauncher t2}
3. 总结
- “singleTask”模式启动的Activity,在启动时,会先在系统中查找属性值affinity等于它的属性值taskAffinity的任务是否存在:如果存在,则在该任务中启动;如果不存在,则会在新任务中启动。因此,如果想要设置“singleTask”启动模式的Activity在新的任务中启动,就要为它设置一个独立的taskAffinity的值。(这句话更加具体的一个例子是:App1中启动了一个taskAffinity为com.xx.test的ActivityA,App2中要启动一个taskAffinity为com.xx.test的ActivityB,那么ActivityB会在ActivityA所在的任务中启动)。
- 如果设置了“singleTask”启动模式的Activity不是在新的任务中启动的,它会在已有的任务中查看是否已经存在相应的Activity实例,如果存在,会调用该实例的onNewIntent,并且会把位于这个Activity实例上面的Activit全部结束掉,最终这个Activity实例会位于任务的堆栈顶端。
4. Reference
部分内容参考自“singleTask”模式 切换到新的栈中
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/153241.html原文链接:https://javaforall.cn