在android里,有4种activity的启动模式,分别为:
“standard” (默认)
“singleTop”
“singleTask”
“singleInstance”
在Android应用中, Activity是最核心的组件, 如何生成一个Activity实例, 可以选择不同的启动模式, 即LaunchMode. 启动模式主要包括: standard, singleTop, singleTask, singleInstance.
标准模式在每次启动时, 都会创建实例; 三种单例模式, 会根据情况选择创建还是复用实例. 在Activity启动中, 创建实例的生命周期: onCreate -> onStart -> onResume; 重用实例的生命周期: onNewIntent -> onResume.
在AndroidManifest的Activity中, 使用launchMode属性, 可以设置启动模式, 默认是standard模式; 使用taskAffinity属性, 并添加包名, 可以设置Activity栈, 默认是当前包名, 只能应用于single模式.
希望通过本文, 可以更好的理解Activity的启动模式(LaunchMode).
全面解析Activity启动模式(LaunchMode)
观察Activity栈的脚本, 参考第5点 .
adb shell dumpsys activity | sed -n -e '/Stack #/p' -e '/Running activities/,/Run #0/p'
1. Standard
标准模式, 启动Activity的默认模式, 被启动的Activity 会运行于 启动的Activity 栈, 因此必须使用Activity的Context启动, 不能使用Application, 否则会报错.
如MainActivity启动TestAActivity.
Stack #1:
Running activities (most recent first): TaskRecord{3caa65e3 #2711 A=me.chunyu.spike.wcl_activity_launchmode_demo U=0 sz=2} Run #1: ActivityRecord{36b06e99 u0 me.chunyu.spike.wcl_activity_launchmode_demo/.TestAActivity t2711} Run #0: ActivityRecord{27396226 u0 me.chunyu.spike.wcl_activity_launchmode_demo/.MainActivity t2711} Stack #0: Running activities (most recent first): TaskRecord{27d796c9 #2695 A=com.miui.home U=0 sz=1} Run #0: ActivityRecord{2e5712cb u0 com.miui.home/.launcher.Launcher t2695}
栈内由下到上: MainActivity -> TestAActivity.
2. SingleTop
栈顶复用模式. 只有Activity位于栈顶, 重复启动时, 会使用默认实例, 即单例模式; 如果位于栈内, 则仍然会创建实例.
MainActivity启动TestA, TestA启动TestB, TestB启动自身, TestB是单例. 观察栈内情况, TestB只有一份实例, 第二次创建复用.
Stack #1:
Running activities (most recent first): TaskRecord{12abf566 #2712 A=me.chunyu.spike.wcl_activity_launchmode_demo U=0 sz=3} Run #2: ActivityRecord{187d7ff7 u0 me.chunyu.spike.wcl_activity_launchmode_demo/.TestBActivity t2712} Run #1: ActivityRecord{a551034 u0 me.chunyu.spike.wcl_activity_launchmode_demo/.TestAActivity t2712} Run #0: ActivityRecord{22f9cce4 u0 me.chunyu.spike.wcl_activity_launchmode_demo/.MainActivity t2712}
栈内: MainActivity -> TestAActivity -> TestBActivity
MainActivity启动TestA, TestA启动TestB, TestB启动TestC, TestC启动TestB, TestB是单例. 观察栈内情况, 由于TestC是栈顶, TestC启动TestB, TestB不是栈顶, 重新创建TestB实例, 则保留两份TestB.
Stack #1:
Running activities (most recent first): TaskRecord{1792f5f0 #2715 A=me.chunyu.spike.wcl_activity_launchmode_demo U=0 sz=5} Run #4: ActivityRecord{1e70110b u0 me.chunyu.spike.wcl_activity_launchmode_demo/.TestBActivity t2715} Run #3: ActivityRecord{c7f4dce u0 me.chunyu.spike.wcl_activity_launchmode_demo/.TestCActivity t2715} Run #2: ActivityRecord{254536cd u0 me.chunyu.spike.wcl_activity_launchmode_demo/.TestBActivity t2715} Run #1: ActivityRecord{36b2da15 u0 me.chunyu.spike.wcl_activity_launchmode_demo/.TestAActivity t2715} Run #0: ActivityRecord{3a1c4a6a u0 me.chunyu.spike.wcl_activity_launchmode_demo/.MainActivity t2715}
栈内: MainActivity -> TestAActivity -> TestBActivity ->TestCActivity -> TestBActivity
3. SingleTask
栈内复用模式, 只要Activity在一个栈中存在, 多次调用时, 都不会创建实例, 即单例模式.
情况包含以下几种:
(1) 任务栈不存在, 初次启动SingleTask实例, 会创建任务栈和实例.
MainActivity启动TestA, TestA启动TestB, TestB是SingleTask, 并且任务栈不同. 观察可知, 系统包含两个任务栈, TestB位于其他任务栈中.
Stack #1:
Running activities (most recent first): TaskRecord{d5d53d4 #2727 A=me.chunyu.spike.stack U=0 sz=1} Run #2: ActivityRecord{1d720e55 u0 me.chunyu.spike.wcl_activity_launchmode_demo/.TestBActivity t2727} TaskRecord{a3f797d #2726 A=me.chunyu.spike.wcl_activity_launchmode_demo U=0 sz=2} Run #1: ActivityRecord{ffd689d u0 me.chunyu.spike.wcl_activity_launchmode_demo/.TestAActivity t2726} Run #0: ActivityRecord{192310ac u0 me.chunyu.spike.wcl_activity_launchmode_demo/.MainActivity t2726}
使用taskAffinity属性, 添加新的Activity栈, 与SingleTask配合使用, Standard模式无效.
新任务栈是 me.chunyu.spike.stack .
(2) 任务栈存在, 初次启动SingleTask实例, 会直接入栈, 与Standard模式相同.
(3) 任务栈相同, 再次启动SingleTask实例, 实例会置于栈顶, 并清除其上面实例, 具有clearTop的效果.
MainActivity启动TestA, TestA启动TestB, TestB是SingleTask, TestB启动TestC, TestC重新启动TestB, 则TestC会出栈. 观察可知, TestC出栈, TestB位于栈顶.
Stack #1:
Running activities (most recent first): TaskRecord{18230815 #2737 A=me.chunyu.spike.wcl_activity_launchmode_demo U=0 sz=3} Run #4: ActivityRecord{1126c300 u0 me.chunyu.spike.wcl_activity_launchmode_demo/.TestBActivity t2737} Run #3: ActivityRecord{3114fee8 u0 me.chunyu.spike.wcl_activity_launchmode_demo/.TestAActivity t2737} Run #2: ActivityRecord{f8e235d u0 me.chunyu.spike.wcl_activity_launchmode_demo/.MainActivity t2737}
TestC启动TestB, SingleTask模式, 导致clearTop, TestC出栈.
(4) 任务栈不同, 再次启动SingleTask实例, 会导致任务栈切换, 后台置于前台.
这比较难理解.MainActivity启动TestA, TestA启动TestB(SingleTask实例, 不同任务栈), TestB启动TestC(与B类似), 则MainActivity和TestA相同栈, TestB和TestC相同栈, 此时栈顶是TestC. 按Home键, 再次启动应用, 则默认任务栈会启动, TestA启动, TestA启动TestC. 应用当前状态如下.
Stack #1:
Running activities (most recent first): TaskRecord{1d05e6c9 #2754 A=me.chunyu.spike.stack U=0 sz=2} Run #4: ActivityRecord{3f77e822 u0 me.chunyu.spike.wcl_activity_launchmode_demo/.TestCActivity t2754} TaskRecord{3fe736d0 #2753 A=me.chunyu.spike.wcl_activity_launchmode_demo U=0 sz=2} Run #3: ActivityRecord{15f0470e u0 me.chunyu.spike.wcl_activity_launchmode_demo/.TestAActivity t2753} TaskRecord{1d05e6c9 #2754 A=me.chunyu.spike.stack U=0 sz=2} Run #2: ActivityRecord{181229e6 u0 me.chunyu.spike.wcl_activity_launchmode_demo/.TestBActivity t2754} TaskRecord{3fe736d0 #2753 A=me.chunyu.spike.wcl_activity_launchmode_demo U=0 sz=2} Run #1: ActivityRecord{28628d61 u0 me.chunyu.spike.wcl_activity_launchmode_demo/.MainActivity t2753} TaskRecord{2d646058 #2719 A=com.android.incallui U=0 sz=1}
TestC至于栈顶, 点击Back键, 不是返回TestA(启动TestC的实例), 而是TestB, 即优先返回相同栈的实例. 再次是TestA, 然后是MainActivity, 依次出栈.
4. SingleInstance
单实例模式, 启动时, 系统会为其创造一个单独的任务栈, 以后每次使用, 都会使用这个单例, 直到其被销毁, 属于真正的单例模式.
示例: MainActivity启动TestA, TestA启动TestB(SingleInstance模式),
TestB启动TestC, TestC再启动TestB, 则仍启动上一次的TestB,
TestC合并入默认栈(MainActivity+TestA).
Stack #1:
Running activities (most recent first): TaskRecord{384e3928 #2765 A=me.chunyu.spike.wcl_activity_launchmode_demo U=0 sz=1} Run #3: ActivityRecord{1ffc5b6b u0 me.chunyu.spike.wcl_activity_launchmode_demo/.TestBActivity t2765} TaskRecord{2ad03544 #2764 A=me.chunyu.spike.wcl_activity_launchmode_demo U=0 sz=3} Run #2: ActivityRecord{293d8c37 u0 me.chunyu.spike.wcl_activity_launchmode_demo/.TestCActivity t2764} Run #1: ActivityRecord{158bc0f3 u0 me.chunyu.spike.wcl_activity_launchmode_demo/.TestAActivity t2764} Run #0: ActivityRecord{77691cf u0 me.chunyu.spike.wcl_activity_launchmode_demo/.MainActivity t2764}
5. startActivityForResult
Thx@回调的幸福时光
startActivityForResult不同于startActivity, 使用LaunchMode模式启动Activity时, 也会有一些不同, 可以正常传递数据, 但是无法连续创建自己时, 会生成多份实例.
TestB(singleTask模式)使用startActivity创建自己时, 会使用默认实例, 即单例; 而使用startActivityForResult创建自己时, 会生成一份新的示例.
Stack #1:
Running activities (most recent first): TaskRecord{323200ac #2786 A=me.chunyu.clwang.stack U=0 sz=3} Run #4: ActivityRecord{3f9e14f3 u0 me.chunyu.spike.wcl_activity_launchmode_demo/.TestBActivity t2786} Run #3: ActivityRecord{30d8f17b u0 me.chunyu.spike.wcl_activity_launchmode_demo/.TestBActivity t2786} Run #2: ActivityRecord{11b95b5c u0 me.chunyu.spike.wcl_activity_launchmode_demo/.TestBActivity t2786} TaskRecord{c86e175 #2785 A=me.chunyu.spike.wcl_activity_launchmode_demo U=0 sz=2} Run #1: ActivityRecord{3558d7c4 u0 me.chunyu.spike.wcl_activity_launchmode_demo/.TestAActivity t2785} Run #0: ActivityRecord{1b8620c u0 me.chunyu.spike.wcl_activity_launchmode_demo/.MainActivity t2785}
由此可知, 因为startActivityForResult需要返回值, 会保留实例, 覆盖单例效果.
注意: 4.x版本通过startActivityForResult启动singleTask, 无法正常获取返回值, 参考 .
5.x以上版本修复此问题, 考虑兼容性, 不推荐使用startActivityForResult和singleTask.
启动模式做为Activity的重要属性, 还是需要比较透彻的掌握.