目录
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
总是看到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:
MainActivity-Standard
SecondActivity- SingleTask
步骤:
使用命令:“dumpsys activity activities“,dump Activity栈的信息。
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
TaskRecord{832ddac #32 A=com.maureen.testtwoactivity U=0 StackId=1 sz=1}
Run #0: ActivityRecord{2de94c6 u0 com.maureen.testtwoactivity/.MainActivity t32}
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
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}
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
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}
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
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}
部分内容参考自"singleTask"模式 切换到新的栈中