简体   繁体   English

如果我可以使用android:configChanges =“keyboard | orientation | screenLayout”,为什么我需要setRetainInstance或onSaveInstance?

[英]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.

相关问题 在确定方向时,我应该如何使用android:configChanges - How should I use android:configChanges when determining orientation 为什么不总是使用 android:configChanges="keyboardHidden|orientation"? - Why not use always android:configChanges=“keyboardHidden|orientation”? Android:当我使用 android:configChanges=“orientation|keyboardHidden”> 时,横向模式的视图发生了变化 - Android: View of landscape mode changes when I use android:configChanges=“orientation|keyboardHidden”> Android configChanges方向 - Android configChanges for orientation android:configChanges =“ orientation”与surfaceChanged - android:configChanges=“orientation” with surfaceChanged Android:“方向”和“屏幕布局”之间有什么区别? - Android: What is a difference between 'orientation' and 'screenLayout'? 将人像旋转到风景时,我要关闭力量,我尝试了android:configChanges =“ keyboard | keyboardHidden”并以人像模式工作吗? - Getting Force Close when rotating portrait to landscape and I tried android:configChanges=“keyboard|keyboardHidden” and working in portrait mode? 如何通过configChanges = orientation使用values-land - How to use values-land with configChanges=orientation 如何正确处理android:configChanges =“ orientation | screenSize” - How to properly handle android:configChanges=“orientation|screenSize” 如何在android方向更改中使用不同的布局 - How can I use different layout in android orientation change
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM