[英]Why should I need setRetainInstance or onSaveInstance if I can use android:configChanges=“keyboard|orientation|screenLayout”
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" ? 为什么我需要使用setRetainInstance()
或onSaveInstance()
来保存状态,我可以使用android:configChanges="keyboard|orientation|screenLayout"
并获得相同的“保存状态非UI状态” ? I mean with less headache. 我的意思是减少头痛。
Don't use android:configChanges
. 不要使用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. 它会以微妙的方式破坏事物并阻止Android获得当前配置的正确布局/主题/尺寸等。
onSaveInstanceState()
is completely orthogonal to this: you need to save state so you can restore it if Android kills your process to save memory. onSaveInstanceState()
与此完全正交:您需要保存状态,以便在Android杀死您的进程以节省内存时可以恢复它。 configChagnes
only prevents it from re-creating the activity on rotation, keyboard state changes, etc. configChagnes
仅阻止它在旋转,键盘状态更改等上重新创建活动。
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. setRetainInstance()
适用于您不希望在设备轮换时重新创建的片段等。如果您不调用它,Android将在Bundle中序列化它们的状态,并与父活动一起重新创建它们。
In short, while configChanges
appears to be a 'shortcut' it is not. 简而言之,虽然configChanges
似乎是一个“捷径”,但事实并非如此。 Don't rely on it and save/restore state as necessary using the proper tools for each case. 不要依赖它并根据需要使用适当的工具保存/恢复状态。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.