Activity 在横竖屏切换情况下的生命周期变化

Posted jnienv

tags:

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


title: Activity 在横竖屏切换情况下的生命周期变化
date: 2018-04-26 23:05:57
tags: [Activity]
categories: [Mobile,android]
---

概述

Activity 在横竖屏切换的时候,生命周期是不一样的,本地通过打印 log 的方式,看下区别.测试的机器是 Android6.0 .

不做任何配置的情况下

第一次启动

D/LifeCircleActivity: onCreate() called with: savedInstanceState = [null]Activity对象的地址:[email protected]
D/LifeCircleActivity: onStart() called
D/LifeCircleActivity: onResume() called

第一次切换成横屏


D/LifeCircleActivity: onPause() called
D/LifeCircleActivity: onSaveInstanceState() called with: outState = [Bundle[{android:viewHierarchyState=Bundle[{android:views={[email protected], [email protected], [email protected]}}], key=x}]]
D/LifeCircleActivity: onStop() called
D/LifeCircleActivity: onDestroy() called
D/LifeCircleActivity: onCreate() called with: savedInstanceState = [Bundle[{android:viewHierarchyState=Bundle[{android:views={[email protected], [email protected], [email protected]}}], key=x}]]Activity对象的地址:[email protected]
D/LifeCircleActivity: onStart() called
D/LifeCircleActivity: onRestoreInstanceState() called with: savedInstanceState = [Bundle[{android:viewHierarchyState=Bundle[{android:views={[email protected], [email protected], [email protected]}}], key=x}]]
D/LifeCircleActivity: onResume() called

再切换成竖屏

D/LifeCircleActivity: onPause() called
D/LifeCircleActivity: onSaveInstanceState() called with: outState = [Bundle[{android:viewHierarchyState=Bundle[{android:views={16908290=android.view.AbsSav[email protected], [email protected], [email protected]}}], key=x}]]
D/LifeCircleActivity: onStop() called
D/LifeCircleActivity: onDestroy() called
D/LifeCircleActivity: onCreate() called with: savedInstanceState = [Bundle[{android:viewHierarchyState=Bundle[{android:views={[email protected], [email protected], [email protected]}}], key=x}]]Activity对象的地址:[email protected]
D/LifeCircleActivity: onStart() called
D/LifeCircleActivity: onRestoreInstanceState() called with: savedInstanceState = [Bundle[{android:viewHierarchyState=Bundle[{android:views={[email protected], [email protected], [email protected]}}], key=x}]]
D/LifeCircleActivity: onResume() called

小结

默认情况下,每次旋转屏幕都会销毁当前的Activity对象,同时调用 onSaveInstanceState 方法,保存当前的界面状态;之后重新创建 Activity对象, onCreate 参数不为空,回调 onRestoreInstanceState 方法进行恢复。

配置 configChanges="orientation"

第一次启动

D/LifeCircleActivity: onCreate() called with: savedInstanceState = [null]Activity对象的地址:[email protected]
D/LifeCircleActivity: onStart() called
D/LifeCircleActivity: onResume() called

第一次切换成横屏

D/LifeCircleActivity: onPause() called
D/LifeCircleActivity: onSaveInstanceState() called with: outState = [Bundle[{android:viewHierarchyState=Bundle[{android:views={[email protected], [email protected], [email protected]}}], key=x}]]
D/LifeCircleActivity: onStop() called
D/LifeCircleActivity: onDestroy() called
D/LifeCircleActivity: onCreate() called with: savedInstanceState = [Bundle[{android:viewHierarchyState=Bundle[{android:views={[email protected], [email protected], [email protected]}}], key=x}]]Activity对象的地址:[email protected]
D/LifeCircleActivity: onStart() called
D/LifeCircleActivity: onRestoreInstanceState() called with: savedInstanceState = [Bundle[{android:viewHierarchyState=Bundle[{android:views={[email protected], [email protected], [email protected]}}], key=x}]]
D/LifeCircleActivity: onResume() called

再切换成竖屏

D/LifeCircleActivity: onPause() called
D/LifeCircleActivity: onSaveInstanceState() called with: outState = [Bundle[{android:viewHierarchyState=Bundle[{android:views={[email protected], [email protected], [email protected]}}], key=x}]]
D/LifeCircleActivity: onStop() called
D/LifeCircleActivity: onDestroy() called
D/LifeCircleActivity: onCreate() called with: savedInstanceState = [Bundle[{android:viewHierarchyState=Bundle[{android:views={[email protected], [email protected], [email protected]}}], key=x}]]Activity对象的地址:[email protected]
D/LifeCircleActivity: onStart() called 
D/LifeCircleActivity: onRestoreInstanceState() called with: savedInstanceState = [Bundle[{android:viewHierarchyState=Bundle[{android:views={[email protected], [email protected], [email protected]}}], key=x}]]
D/LifeCircleActivity: onResume() called

小结

配置 orientation 的情况下,和默认情况一致。


配置 configChanges="orientation|screenSize"

根据官方的介绍,这个两个值,在api大于13 之后,应该一起使用

第一次启动

D/LifeCircleActivity: onCreate() called with: savedInstanceState = [null]Activity对象的地址:[email protected]
D/LifeCircleActivity: onStart() called
D/LifeCircleActivity: onResume() called

第一次切换成横屏

D/LifeCircleActivity: onConfigurationChanged() called with: newConfig = [{1.0 ?mcc?mnc zh_CN ldltr sw360dp w640dp h336dp 320dpi nrml long land finger -keyb/v/h -nav/h s.11 themeChanged=0 themeChangedFlags=0}]

再切换成竖屏

D/LifeCircleActivity: onConfigurationChanged() called with: newConfig = [{1.0 ?mcc?mnc zh_CN ldltr sw360dp w360dp h616dp 320dpi nrml long port finger -keyb/v/h -nav/h s.12 themeChanged=0 themeChangedFlags=0}]

小结

当配置了 screenSize 。则不会再销毁重建了,而是回调 onConfigurationChanged 方法。

总结

在不做配置默认的情况下,Activity 是被销毁,然后重新启动的.但是在 manifest 中进行相应的配置之后,就表示 Activity 自行处理配置的更改,将阻止 Activity 的销毁重新启动,而是保持运行状态,并且回调 onConfigurationChanged 方法.官方的建议是万不得已的情况下才能使用.

参考

以上是关于Activity 在横竖屏切换情况下的生命周期变化的主要内容,如果未能解决你的问题,请参考以下文章

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

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

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

面试题:Activity横竖屏切换时的生命周期如何变化

横竖屏切换Activity的生命周期

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