此文是根据官方文档、图书资料及demo测试得出的结论,包括Activity多个页面间跳转的生命周期,各个启动模式的不同,flag的常见用法及对activity任务栈的影响。
Activity生命周期需要注意的地方
- 当Activity长时间在后台运行时,可能会被杀死,再次启动时Activity会被重建,此情况和横竖屏切换场景类似,开发者应该在开发环境下打开横竖屏切换测试,防止异常发生;
- 如果新的Activity是透明主题,则当前Activity不会执行onStop;
- onSaveInstanceState在onStop之前执行,onRestoreInstanceState在onStart之后执行;
- 为防止Activity横竖屏切换时Activity重启,可添加 android:configChanges="orientation|screenSize"
- onStart和onStop,判断Activity是否可见,onResume和onPause,判断Activity是否在前台;
- A启动B,A、B生命周期执行顺序:A会先进入后台,B创建进入前台,A不可见;
A(onPause) --> B(onCreate) --> B(onStart) --> B(onResume) --> A(onSaveInstanceState) ->A(onStop)
Activity启动模式
Standard:标准模式
- 每次都会新创建Activity,并添加到对应的任务栈中;
SingleTop:栈顶复用
-
如果要启动的Activity已经在栈顶,该Activity不会被创建;
* A --> B(SingleTop) * 再次启动B后生命周期: onPause --> onNewIntent --> onResume
-
如果要启动的Activity不在栈顶,跟标准模式完全一致;
* A --> B(SingleTop) --> C * 再次启动B后栈: A --> B(SingleTop) --> C --> B(SingleTop)
SingleTask:栈内复用模式
-
特别注意,配置此模式的Activity并不会位于一个新的任务栈中,只是如果要启动的Activity已经在栈中,会clearTop,让自己置为栈顶;
* A --> B(SingleTask) --> C TaskRecord{ed93b47 #259 A=test.gcoder.io.testactivity, isShadow:false U=0 sz=4} Run #3: ActivityRecord{c1f4817 u0 test.gcoder.io.testactivity/.CActivity, isShadow:false t259} Run #2: ActivityRecord{59e1a72 u0 test.gcoder.io.testactivity/.BActivity, isShadow:false t259} Run #1: ActivityRecord{d7ae33c u0 test.gcoder.io.testactivity/.AActivity, isShadow:false t259} * 再次启动B后任务栈: A --> B(SingleTask) 此时C会被销毁 此时B的生命周期:onPause --> onNewIntent --> onResume TaskRecord{ed93b47 #259 A=test.gcoder.io.testactivity, isShadow:false U=0 sz=3} Run #2: ActivityRecord{59e1a72 u0 test.gcoder.io.testactivity/.BActivity, isShadow:false t259} Run #1: ActivityRecord{d7ae33c u0 test.gcoder.io.testactivity/.AActivity, isShadow:false t259}
SingleInstance:单实例模式
-
此模式的Activity只能位于一个独立任务栈中;
* A --> B(SingleInstance) --> C 此时,A、C位于同一个任务栈,B位于独立的任务栈; TaskRecord{c37f244 #253 A=test.gcoder.io.testactivity, isShadow:false U=0 sz=3} Run #3: ActivityRecord{8d06468 u0 test.gcoder.io.testactivity/.CActivity, isShadow:false t253} TaskRecord{10b2e56 #257 A=test.gcoder.io.testactivity, isShadow:false U=0 sz=1} Run #2: ActivityRecord{eeb0317 u0 test.gcoder.io.testactivity/.BActivity, isShadow:false t257} TaskRecord{c37f244 #253 A=test.gcoder.io.testactivity, isShadow:false U=0 sz=3} Run #1: ActivityRecord{e25c532 u0 test.gcoder.io.testactivity/.AActivity, isShadow:false t253} * 再次启动B后栈: A --> C --> B(SingleInstance) 此时,B调整为前台任务栈,A、C的任务栈调整为后台任务栈; 此时,B的生命周期:onPause --> onNewIntent --> onResume TaskRecord{10b2e56 #257 A=test.gcoder.io.testactivity, isShadow:false U=0 sz=1} Run #3: ActivityRecord{eeb0317 u0 test.gcoder.io.testactivity/.BActivity, isShadow:false t257} TaskRecord{c37f244 #253 A=test.gcoder.io.testactivity, isShadow:false U=0 sz=3} Run #2: ActivityRecord{8d06468 u0 test.gcoder.io.testactivity/.CActivity, isShadow:false t253} Run #1: ActivityRecord{e25c532 u0 test.gcoder.io.testactivity/.AActivity, isShadow:false t253}
怎么确定Activity位于哪个任务栈
除SingleInstance模式的activity外,其它所有模式的activity都会被添加到默认的任务栈中,默认任务栈taskAffinity为包名;
可以通过指定taskAffinity让activity运行在特定的任务栈中,必须配合FLAG_ACTIVITY_NEW_TASK才会生效;
-
除SingleInstance模式的activity外,启动普通的activity都被添加到启动此activity所用的activity相同的任务栈中,除非同时指定taskAffinity和FLAG_ACTIVITY_NEW_TASK,会运行在与taskAffinity同名的任务栈中;
* A --> B(taskAffinity=test.gcoder.io.newtask flag=FLAG_ACTIVITY_NEW_TASK) TaskRecord{99b9ba5 #269 A=test.gcoder.io.newtask, isShadow:false U=0 sz=1} Run #2: ActivityRecord{5ba0ca2 u0 test.gcoder.io.testactivity/.BActivity, isShadow:false t269} TaskRecord{ab040b2 #267 A=test.gcoder.io.testactivity, isShadow:false U=0 sz=2} Run #1: ActivityRecord{1ddfc0 u0 test.gcoder.io.testactivity/.AActivity, isShadow:false t267} * A --> B(taskAffinity=test.gcoder.io.newtask flag=FLAG_ACTIVITY_NEW_TASK)--> C TaskRecord{99b9ba5 #269 A=test.gcoder.io.newtask, isShadow:false U=0 sz=2} Run #3: ActivityRecord{199ec49 u0 test.gcoder.io.testactivity/.CActivity, isShadow:false t269} Run #2: ActivityRecord{5ba0ca2 u0 test.gcoder.io.testactivity/.BActivity, isShadow:false t269} TaskRecord{ab040b2 #267 A=test.gcoder.io.testactivity, isShadow:false U=0 sz=2} Run #1: ActivityRecord{1ddfc0 u0 test.gcoder.io.testactivity/.AActivity, isShadow:false t267}
Context对Activity任务栈的影响
经过对比,使用Activity Context启动与使用Application Context启动Activity无任何区别,都是在一个任务栈中;
-
使用Activity Context启动
* A --> B(FLAG_ACTIVITY_NEW_TASK) TaskRecord{6a79d7e #261 A=test.gcoder.io.testactivity, isShadow:false U=0 sz=3} Run #2: ActivityRecord{8754172 u0 test.gcoder.io.testactivity/.BActivity, isShadow:false t261} Run #1: ActivityRecord{43b510 u0 test.gcoder.io.testactivity/.AActivity, isShadow:false t261} * A --> B(FLAG_ACTIVITY_NEW_TASK)-->C TaskRecord{6a79d7e #261 A=test.gcoder.io.testactivity, isShadow:false U=0 sz=4} Run #3: ActivityRecord{4095453 u0 test.gcoder.io.testactivity/.CActivity, isShadow:false t261} Run #2: ActivityRecord{8754172 u0 test.gcoder.io.testactivity/.BActivity, isShadow:false t261} Run #1: ActivityRecord{43b510 u0 test.gcoder.io.testactivity/.AActivity, isShadow:false t261}
-
使用Application Context启动
* A --> B(FLAG_ACTIVITY_NEW_TASK) TaskRecord{eb6f24d #262 A=test.gcoder.io.testactivity, isShadow:false U=0 sz=3} Run #2: ActivityRecord{215a79a u0 test.gcoder.io.testactivity/.BActivity, isShadow:false t262} Run #1: ActivityRecord{7e22051 u0 test.gcoder.io.testactivity/.AActivity, isShadow:false t262} * A --> B(FLAG_ACTIVITY_NEW_TASK)--> C TaskRecord{eb6f24d #262 A=test.gcoder.io.testactivity, isShadow:false U=0 sz=4} Run #3: ActivityRecord{dac68e6 u0 test.gcoder.io.testactivity/.CActivity, isShadow:false t262} Run #2: ActivityRecord{215a79a u0 test.gcoder.io.testactivity/.BActivity, isShadow:false t262} Run #1: ActivityRecord{7e22051 u0 test.gcoder.io.testactivity/.AActivity, isShadow:false t262} * 再次启动B后:A --> B(FLAG_ACTIVITY_NEW_TASK) --> C --> B(FLAG_ACTIVITY_NEW_TASK) TaskRecord{eb6f24d #262 A=test.gcoder.io.testactivity, isShadow:false U=0 sz=5} Run #4: ActivityRecord{2ec8ea1 u0 test.gcoder.io.testactivity/.BActivity, isShadow:false t262} Run #3: ActivityRecord{c30b233 u0 test.gcoder.io.testactivity/.CActivity, isShadow:false t262} Run #2: ActivityRecord{e88653e u0 test.gcoder.io.testactivity/.BActivity, isShadow:false t262} Run #1: ActivityRecord{177c4a8 u0 test.gcoder.io.testactivity/.AActivity, isShadow:false t262}
Flag对Activity任务栈的影响
FLAG_ACTIVITY_NEW_TASK
- 只添加此Flag的Activity不会被添加新的任务栈中,此时跟SingleTask无任何关系,也不具备SingleTask同一个任务栈中唯一的特性及clearTop的特性;
- 此Flag可被用于非Activity类型的Context启动新Activity;
- 此Flag与其它Flag混合使用会有不同的效果;
FLAG_ACTIVITY_MULTIPLE_TASK
- 此Flag一般不单独使用,需要配合FLAG_ACTIVITY_NEW_TASK使用;
FLAG_ACTIVITY_MULTIPLE_TASK | FLAG_ACTIVITY_NEW_TASK
单独使用时,会检索已经存在的任务栈,如果启动的activity对应的任务栈存在就不会创建,不存在才会创建新的任务栈;
-
同时使用时,会强制创建新的任务栈,并将启动的activity放在新的任务栈中, 后续使用此activity启动的标准模式的activity会运行在此栈中,而不是默认栈中;
* A --> B(FLAG_ACTIVITY_MULTIPLE_TASK|FLAG_ACTIVITY_NEW_TASK) TaskRecord{4144b1a0 #6 A test.gcoder.io.testactivity U 0} Run #3: ActivityRecord{413fd7a8 test.gcoder.io.testactivity/.BActivity} TaskRecord{41548560 #5 A test.gcoder.io.testactivity U 0} Run #2: ActivityRecord{41562330 test.gcoder.io.testactivity/.AActivity} * A --> B(FLAG_ACTIVITY_MULTIPLE_TASK|FLAG_ACTIVITY_NEW_TASK) --> C TaskRecord{4144b1a0 #6 A test.gcoder.io.testactivity U 0} Run #4: ActivityRecord{41604278 test.gcoder.io.testactivity/.CActivity} Run #3: ActivityRecord{413fd7a8 test.gcoder.io.testactivity/.BActivity} TaskRecord{41548560 #5 A test.gcoder.io.testactivity U 0} Run #2: ActivityRecord{41562330 test.gcoder.io.testactivity/.AActivity} * A --> B(FLAG_ACTIVITY_MULTIPLE_TASK|FLAG_ACTIVITY_NEW_TASK) --> C --> B(FLAG_ACTIVITY_MULTIPLE_TASK|FLAG_ACTIVITY_NEW_TASK) TaskRecord{41434a90 #7 A test.gcoder.io.testactivity U 0} Run #5: ActivityRecord{413a6710 test.gcoder.io.testactivity/.BActivity} TaskRecord{4144b1a0 #6 A test.gcoder.io.testactivity U 0} Run #4: ActivityRecord{41604278 test.gcoder.io.testactivity/.CActivity} Run #3: ActivityRecord{413fd7a8 test.gcoder.io.testactivity/.BActivity} TaskRecord{41548560 #5 A test.gcoder.io.testactivity U 0} Run #2: ActivityRecord{41562330 test.gcoder.io.testactivity/.AActivity}
FLAG_ACTIVITY_SINGLE_TOP
- 同SingleTop
FLAG_ACTIVITY_CLEAR_TOP
此Flag单独使用时,如果启动的Activity存在对应的任务栈中,只是会清除任务栈中要启动的Activity之上所有的Activity,要启动的Activity会先销毁后重新创建;
-
此Flag单独使用时,如果启动的Activity不存在对应的任务栈中,则创建并添加到对应的任务栈中;
* A --> B(FLAG_ACTIVITY_CLEAR_TOP) --> C TaskRecord{166703a #289 A=test.gcoder.io.testactivity, isShadow:false U=0 sz=4} Run #3: ActivityRecord{374b0ff u0 test.gcoder.io.testactivity/.CActivity, isShadow:false t289} Run #2: ActivityRecord{15086 u0 test.gcoder.io.testactivity/.BActivity, isShadow:false t289} Run #1: ActivityRecord{da843b0 u0 test.gcoder.io.testactivity/.AActivity, isShadow:false t289} * 再次启动B(FLAG_ACTIVITY_CLEAR_TOP) * 生命周期:C会销毁,B会先销毁后创建 C(onPause) B(onDestroy)-->B(onCreate)-->B(onStart)-->B(onResume) C(onStop)-->C(onDestroy) TaskRecord{166703a #289 A=test.gcoder.io.testactivity, isShadow:false U=0 sz=3} Run #2: ActivityRecord{6420128 u0 test.gcoder.io.testactivity/.BActivity, isShadow:false t289} Run #1: ActivityRecord{da843b0 u0 test.gcoder.io.testactivity/.AActivity, isShadow:false t289}
FLAG_ACTIVITY_CLEAR_TOP | FLAG_ACTIVITY_NEW_TASK
如果要启动的Activity在对应的任务栈中,则会销毁任务栈顶部的所有 Activity,并通过 onNewIntent() 将此 Intent 传递给 Activity 已恢复的实例(现在位于顶部),而不是启动该 Activity 的新实例;
-
如果启动的Activity没有在对应的任务栈中,则创建新的任务栈,创建Activity并添加到新的任务栈中;
* A --> B(FLAG_ACTIVITY_MULTIPLE_TASK | FLAG_ACTIVITY_CLEAR_TOP) TaskRecord{413377c #286 A=test.gcoder.io.newtask, isShadow:false U=0 sz=1} Run #2: ActivityRecord{a58be9a u0 test.gcoder.io.testactivity/.BActivity, isShadow:false t286} TaskRecord{cfe9805 #285 A=test.gcoder.io.testactivity, isShadow:false U=0 sz=2} Run #1: ActivityRecord{b5a1c24 u0 test.gcoder.io.testactivity/.AActivity, isShadow:false t285} * A --> B(FLAG_ACTIVITY_MULTIPLE_TASK | FLAG_ACTIVITY_CLEAR_TOP)--> C TaskRecord{413377c #286 A=test.gcoder.io.newtask, isShadow:false U=0 sz=2} Run #3: ActivityRecord{9f7c8df u0 test.gcoder.io.testactivity/.CActivity, isShadow:false t286} Run #2: ActivityRecord{a58be9a u0 test.gcoder.io.testactivity/.BActivity, isShadow:false t286} TaskRecord{cfe9805 #285 A=test.gcoder.io.testactivity, isShadow:false U=0 sz=2} Run #1: ActivityRecord{b5a1c24 u0 test.gcoder.io.testactivity/.AActivity, isShadow:false t285} * 再次启动B(FLAG_ACTIVITY_MULTIPLE_TASK | FLAG_ACTIVITY_CLEAR_TOP) * 生命周期:C会销毁,B会先销毁后创建 C(onPause) B(onDestroy)-->B(onCreate)-->B(onStart)-->B(onResume) C(onStop)-->C(onDestroy) TaskRecord{413377c #286 A=test.gcoder.io.newtask, isShadow:false U=0 sz=1} Run #2: ActivityRecord{ff16c57 u0 test.gcoder.io.testactivity/.BActivity, isShadow:false t286} TaskRecord{cfe9805 #285 A=test.gcoder.io.testactivity, isShadow:false U=0 sz=2} Run #1: ActivityRecord{b5a1c24 u0 test.gcoder.io.testactivity/.AActivity, isShadow:false t285}
注:如果指定 Activity 的启动模式为 "standard",则该 Activity 也会从堆栈中移除,并在其位置启动一个新实例,以便处理传入的 Intent。 这是因为当启动模式为 "standard" 时,将始终为新 Intent 创建新实例。
参考资料:
- Android开发艺术探索第一章;
- Android官方文档
本文作者:gcoder.io
本文链接:http://gcoder-io.github.io/2016/10/05/android-activity-lifecycle-launchmode/
版权声明: 本博客所有文章均为原创,转载请注明作者及出处