Activity横竖屏切换生命周期
网上大多博客结论,清单文件Activity未配置configChanges属性,切到横屏后,再切回竖屏时,会走两遍onPause——onSaveInstanceState——onStop——onDestroy——onCreate——onStart——onRestoreInstanceState——onResume 生命周期方法。本人在Android系统不同版本经过测试发现,实际情况和网上大多博客描述不同,特记录下实际测试情况。有误解之处,还望指出,共同探讨。
Android8.0及以上
- 没配置configChanges属性,切到横屏和再切到竖屏,生命周期调用顺序都表现为:
onPause
onSaveInstanceState
onStop
onDestroy
onCreate
onStart
onRestoreInstanceState
onResume
- 只要随意配置了configChanges属性,切到横屏和再切到竖屏,生命周期调用顺序都表现为:
onConfigurationChanged
Android7.0、7.1.1
- 没配置configChanges属性和没配置全configChanges属性是orientation|keyboardHidden|screenSize,切到横屏和再切到竖屏,生命周期调用顺序都表现为:
onConfigurationChanged
onPause
onSaveInstanceState
onStop
onDestroy
onCreate
onStart
onRestoreInstanceState
onResume
- 配置了configChanges属性是orientation|keyboardHidden|screenSize,切到横屏和再切到竖屏,生命周期调用顺序都表现为:
onConfigurationChanged
Android6.0及以下
- 未配置configChanges属性和未配置全configChanges属性是orientation|keyboardHidden|screenSize,切到横屏和再切到竖屏,生命周期调用顺序都表现为:
onPause
onSaveInstanceState
onStop
onDestroy
onCreate
onStart
onRestoreInstanceState
onResume
- 配置了configChanges属性是orientation|keyboardHidden|screenSize,切到横屏和再切到竖屏,生命周期调用顺序都表现为:
onConfigurationChanged