我为什么要需要setRetainInstance或onSaveInstance如果我可以使用安卓configChanges ="键盘|方向|屏幕布置" [英] Why should I need setRetainInstance or onSaveInstance if I can use android:configChanges="keyboard|orientation|screenLayout"
问题描述
我为什么要需要使用 setRetainInstance()
或 onSaveInstance()
保存状态,我可以使用安卓configChanges =键盘|方向|屏幕布置
并获得samething的节能状态非UI状态的?我的意思是用更少的头痛。
Why should I need to use setRetainInstance()
or onSaveInstance()
to save state and I can use android:configChanges="keyboard|orientation|screenLayout"
and get the samething "saving state non-UI state"? I mean with less headache.
推荐答案
不要使用安卓configChanges
。这将打破东西在微妙的方式,将prevent的Android从得到适当的布局/主题/尺寸等当前配置。
Don't use android:configChanges
. It will break things in subtle ways and will prevent Android from getting the proper layout/theme/dimensions, etc. for the current configuration.
的onSaveInstanceState()
完全是垂直的:你需要保存状态,这样你就可以,如果机器人杀死你的进程,以节省内存恢复。 configChagnes
仅从重新创建的旋转活动,键盘状态变化等prevents它
onSaveInstanceState()
is completely orthogonal to this: you need to save state so you can restore it if Android kills your process to save memory. configChagnes
only prevents it from re-creating the activity on rotation, keyboard state changes, etc.
setRetainInstance()
是指您不希望在设备旋转等重新创建如果你不叫它,Android将序列化它们的状态片段在一个包,而且随着父活动重新创建。
setRetainInstance()
is for fragments that you don't want re-created on device rotation, etc. If you don't call it, Android will serialize their state in a Bundle, and re-create them along with the parent activity.
在短期,而 configChanges
看起来是一个捷径事实并非如此。不要依赖于它,保存/使用适当的工具,每一种情况下恢复必要的状态。
In short, while configChanges
appears to be a 'shortcut' it is not. Don't rely on it and save/restore state as necessary using the proper tools for each case.
这篇关于我为什么要需要setRetainInstance或onSaveInstance如果我可以使用安卓configChanges ="键盘|方向|屏幕布置"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!