Fragment和Activity生命周期以及横竖屏切换对生命周期的影响

Posted robert_chao

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Fragment和Activity生命周期以及横竖屏切换对生命周期的影响相关的知识,希望对你有一定的参考价值。



Fragment生命周期图解,fragment和activity生命周期对比




生命周期变化 : 切换到该Fragment

06-10 14:26:35.095: D/FragmentDemo(7649): onAttach
06-10 14:26:35.095: D/FragmentDemo(7649): onCreate
06-10 14:26:35.095: D/FragmentDemo(7649): onCreateView
06-10 14:26:35.100: D/FragmentDemo(7649): onActivityCreated
06-10 14:26:35.120: D/FragmentDemo(7649): onStart
06-10 14:26:35.120: D/FragmentDemo(7649): onResume

屏幕灭掉:
06-10 14:27:35.185: D/FragmentDemo(7649): onPause
06-10 14:27:35.205: D/FragmentDemo(7649): onSaveInstanceState
06-10 14:27:35.205: D/FragmentDemo(7649): onStop

屏幕解锁
06-10 14:33:13.240: D/FragmentDemo(7649): onStart
06-10 14:33:13.275: D/FragmentDemo(7649): onResume

切换到其他Fragment:
06-10 14:33:33.655: D/FragmentDemo(7649): onPause
06-10 14:33:33.655: D/FragmentDemo(7649): onStop
06-10 14:33:33.660: D/FragmentDemo(7649): onDestroyView

切换回本身的Fragment:
06-10 14:33:55.820: D/FragmentDemo(7649): onCreateView
06-10 14:33:55.825: D/FragmentDemo(7649): onActivityCreated
06-10 14:33:55.825: D/FragmentDemo(7649): onStart
06-10 14:33:55.825: D/FragmentDemo(7649): onResume

回到桌面
06-10 14:34:26.590: D/FragmentDemo(7649): onPause
06-10 14:34:26.880: D/FragmentDemo(7649): onSaveInstanceState
06-10 14:34:26.880: D/FragmentDemo(7649): onStop

回到应用
06-10 14:36:51.940: D/FragmentDemo(7649): onStart
06-10 14:36:51.940: D/FragmentDemo(7649): onResume

退出应用
06-10 14:37:03.020: D/FragmentDemo(7649): onPause
06-10 14:37:03.155: D/FragmentDemo(7649): onStop
06-10 14:37:03.155: D/FragmentDemo(7649): onDestroyView
06-10 14:37:03.165: D/FragmentDemo(7649): onDestroy

06-10 14:37:03.165: D/FragmentDemo(7649): onDetach


新建一个Activity,在各个生命周期中输出log
1、按crtl+f12切换成横屏时
onSaveInstanceState-->
onPause-->
onStop-->
onDestroy-->
onCreate-->
onStart-->
onRestoreInstanceState-->
onResume-->

2、再按crtl+f12切换成竖屏时,发现打印了两次相同的log
onSaveInstanceState-->
onPause-->
onStop-->
onDestroy-->
onCreate-->
onStart-->
onRestoreInstanceState-->
onResume-->
onSaveInstanceState-->
onPause-->
onStop-->
onDestroy-->
onCreate-->
onStart-->
onRestoreInstanceState-->
onResume-->

3、修改androidManifest.xml,把该Activity添加 Android:configChanges="orientation",执行步骤2,横屏切换为竖屏
onSaveInstanceState-->
onPause-->
onStop-->
onDestroy-->
onCreate-->
onStart-->
onRestoreInstanceState-->
onResume-->

4、再执行步骤2,竖屏切换为横屏,发现不会再打印相同信息,但多打印了一行onConfigChanged
onSaveInstanceState-->
onPause-->
onStop-->
onDestroy-->
onCreate-->
onStart-->
onRestoreInstanceState-->
onResume-->
onConfigurationChanged-->

5、把步骤3的android:configChanges="orientation" 改成 android:configChanges="orientation|keyboardHidden",执行步骤2,竖屏切换横屏,就只打印onConfigChanged
onConfigurationChanged-->

8、再次执行步骤2,横屏切换回竖屏
onConfigurationChanged-->

 总结:
1、不设置Activity的android:configChanges时,切屏会重新调用各个生命周期,切横屏时会执行一次,切竖屏时会执行两次

2、设置Activity的android:configChanges="orientation"时,切屏还是会重新调用各个生命周期,切横、竖屏时只会执行一次

3、设置Activity的android:configChanges="orientation|keyboardHidden"时,切屏不会重新调用各个生命周期,只会执行onConfigurationChanged方法


以上是关于Fragment和Activity生命周期以及横竖屏切换对生命周期的影响的主要内容,如果未能解决你的问题,请参考以下文章

android知识点

Activity横竖屏切换生命周期变化

Activity的横竖屏生命周期

横竖屏切换Activity的生命周期

Activity 横竖屏生命周期

横竖屏切换时activity生命周期变化的总结