码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2016-06-11 10:37:20      阅读:194      评论:0      收藏:0      [点我收藏+]

标签:



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


技术分享技术分享


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

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

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

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

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

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

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

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

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

05-21 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生命周期以及横竖屏切换对生命周期的影响

标签:

原文地址:http://blog.csdn.net/robertcpp/article/details/51628794

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!