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

横竖屏切换


1 切换到横屏
onSaveInstanceState
onPause
onStop
onDestroy
onCreate
onStart
onRestoreInstanceState
onResume


2 切换到竖屏,销毁了两次
onSaveInstanceState
onPause
onStop
onDestroyonCreate
onStart
onRestoreInstanceState
onResume
onSaveInstanceState
onPause
onStop
onDestroy
onCreate
onStart
onRestoreInstanceState
onResume




3 修改AndroidManifest.xml,把该Activity添加 android:configChanges="orientation",切横屏,只销毁一次。


onSaveInstanceState
onPause
onStop
onDestroy
onCreate
onStart
onRestoreInstanceState
onResume


4 再切回竖屏,发现不会再打印相同信息,但多打印了一行onConfigChanged


onSaveInstanceState
onPause
onStop
onDestroy
onCreate
onStart
onRestoreInstanceState
onResume
onConfigurationChanged


5 更改 android:configChanges="orientation" 改成 android:configChanges="orientation|keyboardHidden",切横屏,就只打印onConfigChanged


onConfigurationChanged


6 切回竖屏


onConfigurationChanged
onConfigurationChanged


总结:


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


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


3、设置Activity的android:configChanges="orientation|keyboardHidden"时,切屏不会重新调用各个生命周期,只会执行onConfigurationChanged方法。(执行这个方法,必须设置sdk version 大于等于13.0)



附:设置android:screenOrientation="portrait"则无论手机如何变动,拥有这个属性的activity都将是竖屏显示。
同样的,android:screenOrientation="landscape",为横屏显示。

你可能感兴趣的:(Android)