使用 `onRetainCustomNonConfigurationInstance` 跨配置更改保留数据
Using `onRetainCustomNonConfigurationInstance` to retain data across configuration changes
我为 Android 编程已经有一段时间了,我仍在寻找解决方案以在配置更改时保留数据。除了在 onSaveInstanceState
中将 Parcelable
保存到 Activity 的 Bundle
之外,文档建议使用 Fragment
并将 setRetainInstance
标志设置为 true。
但我刚刚遇到一些使用 onRetainCustomNonConfigurationInstance
来保存任意对象的代码(以一种奇特的方式,但本质上是没有引用 Activity
等的大对象)。没见过这种方法用过,所以有点疑惑:
- 调用此方法来存储任意对象是否安全(从某种意义上说,我可以非常确定它会被调用,并且不会很快 deprecated/removed)?
- 此方法与
onRetainNonConfigurationInstance()
有何不同,onRetainNonConfigurationInstance()
也应该 return Object
,本质上应该类似?
- 出于某种原因,使用保留片段是否更好?
作为奖励,我将不胜感激任何其他保存对象状态的提示或解决方案,例如 AsyncTask
、Observable
、视图的演示者并继续
Is this method safe to call to store arbitrary objects (in a sense
that I can be pretty sure it's gonna get called, and that it won't be
deprecated/removed anytime soon)?
onRetainCustomNonConfigurationInstance()
是一种相对较新的方法,并未弃用。我真的认为它不会很快消失,因为没有理由为了删除它而引入新的东西。您可以放心使用。
How is this method different from onRetainNonConfigurationInstance(),
which also should return Object, and in essence should work similarly?
onRetainNonConfigurationInstance()
始终 return 内部 NonConfigurationInstances
class 的实例,具有保留的片段、加载器等状态。您不能(也不应该)更改此系统行为。这就是为什么该方法是 final
并且您不能覆盖它的原因。
如果您想保留您的自定义实例,您需要从那里覆盖 onRetainCustomNonConfigurationInstance()
和 return 它。
事实上,onRetainNonConfigurationInstance()
调用 onRetainCustomNonConfigurationInstance()
并保留与其他状态(如保留的片段和加载程序)一起重新调整的实例。
Is using retained fragment still better, for some reason?
这完全取决于您的用例和偏好。逻辑可能是这样的。如果你的activity只是控制fragments,里面没有其他特殊的逻辑,那么使用retained fragments会更简单。如果你的 activity 有东西要保留,那么你可以放心地使用 onRetainCustomNonConfigurationInstance()
方法。至于现在,在这两种情况下,状态仍然由旧的和已弃用的 onRetainNonConfigurationInstance()
方法保留。
p.s。关于存储状态的额外问题,我宁愿建议查看 onSaveInstanceState() 方法。它用于存储状态。
更新: AndroidX release from November 5, 2018 已弃用该方法,并附有以下注释:onRetainCustomNonConfigurationInstance 已弃用。使用 ViewModel 来存储需要在配置更改后继续存在的对象。
我为 Android 编程已经有一段时间了,我仍在寻找解决方案以在配置更改时保留数据。除了在 onSaveInstanceState
中将 Parcelable
保存到 Activity 的 Bundle
之外,文档建议使用 Fragment
并将 setRetainInstance
标志设置为 true。
但我刚刚遇到一些使用 onRetainCustomNonConfigurationInstance
来保存任意对象的代码(以一种奇特的方式,但本质上是没有引用 Activity
等的大对象)。没见过这种方法用过,所以有点疑惑:
- 调用此方法来存储任意对象是否安全(从某种意义上说,我可以非常确定它会被调用,并且不会很快 deprecated/removed)?
- 此方法与
onRetainNonConfigurationInstance()
有何不同,onRetainNonConfigurationInstance()
也应该 returnObject
,本质上应该类似? - 出于某种原因,使用保留片段是否更好?
作为奖励,我将不胜感激任何其他保存对象状态的提示或解决方案,例如 AsyncTask
、Observable
、视图的演示者并继续
Is this method safe to call to store arbitrary objects (in a sense that I can be pretty sure it's gonna get called, and that it won't be deprecated/removed anytime soon)?
onRetainCustomNonConfigurationInstance()
是一种相对较新的方法,并未弃用。我真的认为它不会很快消失,因为没有理由为了删除它而引入新的东西。您可以放心使用。
How is this method different from onRetainNonConfigurationInstance(), which also should return Object, and in essence should work similarly?
onRetainNonConfigurationInstance()
始终 return 内部 NonConfigurationInstances
class 的实例,具有保留的片段、加载器等状态。您不能(也不应该)更改此系统行为。这就是为什么该方法是 final
并且您不能覆盖它的原因。
如果您想保留您的自定义实例,您需要从那里覆盖 onRetainCustomNonConfigurationInstance()
和 return 它。
事实上,onRetainNonConfigurationInstance()
调用 onRetainCustomNonConfigurationInstance()
并保留与其他状态(如保留的片段和加载程序)一起重新调整的实例。
Is using retained fragment still better, for some reason?
这完全取决于您的用例和偏好。逻辑可能是这样的。如果你的activity只是控制fragments,里面没有其他特殊的逻辑,那么使用retained fragments会更简单。如果你的 activity 有东西要保留,那么你可以放心地使用 onRetainCustomNonConfigurationInstance()
方法。至于现在,在这两种情况下,状态仍然由旧的和已弃用的 onRetainNonConfigurationInstance()
方法保留。
p.s。关于存储状态的额外问题,我宁愿建议查看 onSaveInstanceState() 方法。它用于存储状态。
更新: AndroidX release from November 5, 2018 已弃用该方法,并附有以下注释:onRetainCustomNonConfigurationInstance 已弃用。使用 ViewModel 来存储需要在配置更改后继续存在的对象。