上班无聊验证一下activity的生命周期:
1.点击home键 进入后台
ActivityLife: MainActivity ========= onPause
ActivityLife: MainActivity ========= onSaveInstanceState
ActivityLife: MainActivity ========= onStop
从后台回来:
ActivityLife: MainActivity ========= onRestart
ActivityLife: MainActivity ========= onStart
ActivityLife: MainActivity ========= onResume
2.点击back键:
ActivityLife: MainActivity ========= onCreate
ActivityLife: MainActivity ========= onStart
ActivityLife: MainActivity ========= onResume
ActivityLife: MainActivity ========= onPause
ActivityLife: MainActivity ========= onStop
ActivityLife: MainActivity ========= onDestroy
3,由MainActivity跳转SencondActivity
MainActivity ========= onCreate
MainActivity ========= onStart
MainActivity ========= onResume
ActivityLife: MainActivity ========= onPause
ActivityLife: SencondActivity ========= onCreate
ActivityLife: SencondActivity ========= onStart
ActivityLife: SencondActivity ========= onResume
ActivityLife: MainActivity ========= onSaveInstanceState
ActivityLife: MainActivity ========= onStop
从一个activity跳转到另一个activity时,第一个activity先走onPause,然后第二个activity走启动逻辑,之后第一个activity的onStop会被调用,MainActivity不可见之后会调用onSaveInstanceState保存数据。
4,横竖屏切换时Activity的生命周期
(1)不设置Activity的android:configChanges=""时
由竖屏切到横屏
Activity先销毁后重建
由横屏切回竖屏:
也是先销毁再重建
(2)设置Activity的android:configChanges="orientation"时
8.0的手机切换横竖屏各走一次onConfigurationChanged
7.0的手机切换横屏时
切回竖屏时
7.0的手机会先走onConfigurationChanged在回调各个生命周期
6.0的手机切换横竖屏时不会走onConfigurationChanged其他和7.0一致
(3)设置Activity的android:configChanges="orientation|keyboardHidden"时
只会回调onConfigurationChanged回调。
简单记录下,方便以后复习的时候用。