-
Notifications
You must be signed in to change notification settings - Fork 778
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
2019-08-06:说下切换横竖屏时Activity的生命周期? #115
Comments
1.AndroidManifest没有设置configChanges属性 onCreate -->onStart-->onResume 切换横屏: onPause -->onSaveInstanceState -->onStop -->onDestroy -->onCreate-->onStart --> onRestoreInstanceState-->onResume -->onPause -->onStop -->onDestroy (Android 6.0 Android 7.0 Android 8.0) 横屏启动: onCreate -->onStart-->onResume 切换竖屏: onPause -->onSaveInstanceState -->onStop -->onDestroy -->onCreate-->onStart --> onRestoreInstanceState-->onResume -->onPause -->onStop -->onDestroy (Android 6.0 Android 7.0 Android 8.0) 总结:没有设置configChanges属性Android 6.0 7.0 8.0 系统手机 表现都是一样的,当前的界面调用onSaveInstanceState走一遍流程,然后重启调用onRestoreInstanceState再走一遍完整流程,最终destory。 2.AndroidManifest设置了configChanges android:configChanges="orientation" onCreate -->onStart-->onResume 切换横屏: onPause -->onSaveInstanceState -->onStop -->onDestroy -->onCreate-->onStart --> onRestoreInstanceState-->onResume -->onPause -->onStop -->onDestroy (Android 6.0) onConfigurationChanged-->onPause -->onSaveInstanceState -->onStop -->onDestroy --> (Android 7.0) onConfigurationChanged (Android 8.0) 横屏启动: onCreate -->onStart-->onResume 切换竖屏: onPause -->onSaveInstanceState -->onStop -->onDestroy -->onCreate-->onStart --> onRestoreInstanceState--> onResume -->onPause -->onStop -->onDestroy (Android 6.0 ) onConfigurationChanged-->onPause -->onSaveInstanceState -->onStop -->onDestroy --> (Android 7.0) onConfigurationChanged (Android 8.0) 总结:设置了configChanges属性为orientation之后,Android6.0 同没有设置configChanges情况相同,完整的走完了两个生命周期,调用了onSaveInstanceState和onRestoreInstanceState方法;Android 7.0则会先回调onConfigurationChanged方法,剩下的流程跟Android 6.0 保持一致;Android 8.0 系统更是简单, 3.AndroidManifest设置了configChanges 总结:设置android:configChanges="orientation|keyboardHidden|screenSize" 则都不会调用Activity的其他生命周期方法,只会调用onConfigurationChanged方法。 4.AndroidManifest设置了configChanges 总结:没有了keyboardHidden跟3是相同的,orientation代表横竖屏切换 screenSize代表屏幕大小发生了改变, 5.AndroidManifest设置了configChanges 总结:跟只设置了orientation属性相同,Android6.0 Android7.0会回调生命周期的方法,Android8.0则只回调onConfigurationChanged。说明如果设置了orientation 和 screenSize 都不会走生命周期的方法,keyboardHidden不影响。 1.不设置configChanges属性不会回调onConfigurationChanged,且切屏的时候会回调生命周期方法。 注:这里只选择了Android部分系统的手机做测试,由于不同系统的手机品牌也不相同,可能略微会有区别。 另: 获取屏幕状态(int ORIENTATION_PORTRAIT = 1; 竖屏 int ORIENTATION_LANDSCAPE = 2; 横屏) configChanges属性
screenOrientation属性
|
竖屏: onCreat->onStart->onSaveInstanceState->onResume. 但是,我们在如果配置这个属性:android:configChanges="orientation|keyboardHidden|screenSize" |
补充一个生命周期的方法, onRetainNonConfigurationInstance 和 getLastNonConfigurationInstance,在config变化引起act 重建时会触发,但是要注意,一定要是 config 变化引起的才会调用,以前一度以为异常回收也会调用,然而并不是 |
这个方法 现在不能继承了。除非直接继承activity |
android10和上面各位小伙伴说的不完全一致: |
onPause- onSaveInstanceState -onStop- onDestroy- onCreate -onStart- onRestoreInstanceState- onResume |
分别就配置android:configChanges="orientation|keyboardHidden"和不配置进行说明。
The text was updated successfully, but these errors were encountered: