简体   繁体   English

使用onRetainCustomNonConfigurationInstance保留跨配置更改的数据

[英]Using `onRetainCustomNonConfigurationInstance` to retain data across configuration changes

I've been programming for Android for some time, and I'm still looking for solutions to retain data over configuration changes. 我已经为Android编程了一段时间了,但我仍在寻找解决方案以通过配置更改保留数据。 Aside from saving Parcelable s to Activity's Bundle in onSaveInstanceState docs are suggesting using Fragment with setRetainInstance flag set to true. 除了节省Parcelable s到活动的BundleonSaveInstanceState文档使用暗示FragmentsetRetainInstance标志设置为true。

But I've just come across some code that uses onRetainCustomNonConfigurationInstance to hold arbitrary objects (in a fancy way, but essentially big objects without references to Activity etc.). 但是我刚刚遇到了一些使用onRetainCustomNonConfigurationInstance来保存任意对象的代码(以一种奇特的方式,但是本质上是大对象,而没有引用Activity等)。 I have never seen this method used, so I have some doubts: 我从未见过使用此方法,因此我有一些疑问:

  • 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)? 调用此方法是否安全以存储任意对象(从某种意义上说,我可以确定它会被调用,并且不会在不久的将来被弃用/移除)?
  • How is this method different from onRetainNonConfigurationInstance() , which also should return Object , and in essence should work similarly? 此方法与onRetainNonConfigurationInstance()不同,后者也应返回Object ,并且本质上应该类似地工作?
  • Is using retained fragment still better, for some reason? 由于某些原因,使用保留的片段是否还更好?

As a bonus, I would be grateful for any other tips or solutions to save state of objects like AsyncTask , Observable , view's presenters and go on 作为奖励,我将不胜感激任何其他提示或解决方案来保存对象的状态,例如AsyncTaskObservable ,view的presenters并继续

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() is a relatively new method and it is not deprecated. onRetainCustomNonConfigurationInstance()是一个相对较新的方法,不建议使用。 I would really assume it is not going to disappear soon, because there is no reason for introducing something new just to remove it. 我真的认为它不会很快消失,因为没有理由为了删除它而引入新的东西。 You can use it safely. 您可以安全地使用它。

How is this method different from onRetainNonConfigurationInstance(), which also should return Object, and in essence should work similarly? 此方法与onRetainNonConfigurationInstance()有什么不同,后者也应返回Object,并且本质上应该类似地工作?

onRetainNonConfigurationInstance() always return an instance of inner NonConfigurationInstances class with retained fragments, loaders etc states. onRetainNonConfigurationInstance()始终返回内部NonConfigurationInstances类的实例,该类具有保留的片段,加载程序等状态。 You cannot (and should not) change this system behavior. 您不能(也不应)更改此系统行为。 That's why the method is final and you cannot override it. 这就是为什么该方法是final方法,并且您无法覆盖它的原因。

If you want to retain your custom instance, you need to override onRetainCustomNonConfigurationInstance() and return it from there. 如果要保留自定义实例,则需要重写onRetainCustomNonConfigurationInstance()并从那里返回它。

In fact, onRetainNonConfigurationInstance() calls onRetainCustomNonConfigurationInstance() and retains retuned instance with the other states like retained fragments and loaders. 实际上, onRetainNonConfigurationInstance()调用onRetainCustomNonConfigurationInstance()并使用其他状态(如保留的片段和加载程序onRetainCustomNonConfigurationInstance()保留重新调整的实例。

Is using retained fragment still better, for some reason? 由于某些原因,使用保留的片段是否还更好?

It is rather a matter of your use case and preferences. 这完全取决于您的用例和首选项。 The logic might be like this. 逻辑可能是这样的。 If your activity just controls fragments and has no other special logic in it, then it is easier to use retained fragments. 如果您的活动仅控制片段,并且其中没有其他特殊逻辑,则使用保留的片段会更容易。 If your activity has something to retain, then you can safely use onRetainCustomNonConfigurationInstance() method. 如果您的活动需要保留,则可以安全地使用onRetainCustomNonConfigurationInstance()方法。 As for now, in both cases the state still gets retained by good old and deprecated onRetainNonConfigurationInstance() method. 就目前而言,在这两种情况下,状态仍将由旧的且已过时的onRetainNonConfigurationInstance()方法保留。

ps Regarding the bonus question about storing a state I would rather suggest to look at onSaveInstanceState() method. ps关于存储状态的奖励问题,我宁愿建议查看onSaveInstanceState()方法。 It was intended for storing states. 它旨在存储状态。

Update: AndroidX release from November 5, 2018 deprecated the method with the following note: onRetainCustomNonConfigurationInstance has been deprecated. 更新: 自2018年11月5日起,AndroidX版本已弃用该方法,并带有以下说明: onRetainCustomNonConfigurationInstance已弃用。 Use a ViewModel for storing objects that need to survive configuration changes. 使用ViewModel来存储需要保留配置更改的对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android Webview使用Fragments保留方向变化中的文本数据 - Android Webview using Fragments to Retain text data across orientation changes 我们如何使用无头片段在配置更改中保留简单的状态数组? - How can we retain a simple State Array across Configuration changes using Headless Fragments? 在方向更改中保留xml片段 - retain xml fragment across orientation changes 通过配置更改保留对象的静态模式 - Static pattern to retain objects through configuration changes 在Android配置更改中保存模型的最佳方法是什么? - What's the best way to preserve a model across android configuration changes? 如何跨 Fragment 跟踪数据变化? - How to keep track of data changes across Fragments? 当方向发生变化时,Android webview会保留表单数据 - Android webview retain form data when orientation changes Android Webview - 如何在方向更改时保留文本数据 - Android Webview - How to retain text data when orientation changes 在AndroidX中不推荐使用onRetainCustomNonConfigurationInstance - onRetainCustomNonConfigurationInstance deprecated in AndroidX 使用onRetainCustomNonConfigurationInstance进行服务泄漏 - Service Leaking with onRetainCustomNonConfigurationInstance
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM