1) 两个Activity(A->B)切换(B正常的Activity)的生命周期:onPause(A)->onCreate(B)->onStart(B)->onResume(B)->oStop(A)
这时如果按回退键回退到A onPause(B)->onRestart(A)->onStart(A)->onResume(A)->oStop(B)
如果在切换到B后调用了A.finish(),则会走到onDestory(A),这时点回退键会退出应用
2) 两个Activity(A->B)切换(B透明主题的Activity或是Dialog风格的Acivity)的生命周期:onPause(A)->onCreate(B)->onStart(B)->onResume(B)
这时如果回退到A onPause(B)->onResume(A)->oStop(B)->onDestory(B)
3) Activity(A)启动后点击Home键再回到应用的生命周期:onPause(A)->oStop(A)->onRestart(A)->onStart(A)->onResume(A)
5、横竖屏切换的时候,Activity 各种情况下的生命周期
1)切换横屏时:onSaveInstanceState->onPause->onStop->onDestory->onCreate->onStart->onRestoreInstanceState->onResume
2) 切换竖屏时:会打印两次相同的log
onSaveInstanceState->onPause->onStop->onDestory->onCreate->onStart->onRestoreInstanceState->onResume->onSaveInstanceState->onPause->onStop->onDestory->onCreate->onStart->onRestoreInstanceState->onResume
3) 如果在AndroidMainfest.xml中修改该Activity的属性,添加android:configChanges="orientation"
横竖屏切换,打印的log一样,同1)
4) 如果AndroidMainfest.xml中该Activity中的android:configChanges="orientation|keyboardHidden",则只会打印
onConfigurationChanged->
6、Activity与Fragment之间生命周期比较
Fragment生命周期:onAttach->onCreate->onCreateView->onActivityCreated->onStart->onResume->onPause->onStop->onDestoryView->onDestory->onDetach
切换到该Fragment:onAttach->onCreate->onCreateView->onActivityCreated->onStart->onResume
按下Power键:onPause->onSaveInstanceState->onStop
点亮屏幕解锁:onStart->onRestoreInstanceState->onResume
切换到其他Fragment: onPause->onStop->onDestoryView
切回到该Fragment: onCreateView->onActivityCreated->onStart->onResume
退出应用:onPause->onStop->onDestoryView->onDestory->onDetach
7、Activity上有Dialog的时候按Home键时的生命周期
AlertDialog并不会影响Activity的生命周期,按Home键后才会使Activity走onPause->onStop,AlertDialog只是一个组件,并不会使Activity进入后台。
8、两个Activity 之间跳转时必然会执行的是哪几个方法?
前一个Activity的onPause,后一个Activity的onResume
9、前台切换到后台,然后再回到前台,Activity生命周期回调方法。弹出Dialog,生命值周期回调方法。
1)前台切换到后台,会执行onPause->onStop,再回到前台,会执行onRestart->onStart->onResume
2) 弹出Dialog,并不会影响Activity生命周期