使用onRetainCustomNonConfigurationInstance保留跨配置更改的数据 [英] Using `onRetainCustomNonConfigurationInstance` to retain data across configuration changes

查看:227
本文介绍了使用onRetainCustomNonConfigurationInstance保留跨配置更改的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经为Android编程了一段时间了,但我仍在寻找解决方案以通过配置更改保留数据.除了将Parcelable s保存到onSaveInstanceState文档中的Activity的Bundle中之外,建议使用Fragment并且将setRetainInstance标志设置为true.

I've been programming for Android for some time, and I'm still looking for solutions to retain data over configuration changes. Aside from saving Parcelables to Activity's Bundle in onSaveInstanceState docs are suggesting using Fragment with setRetainInstance flag set to true.

但是我刚刚遇到了一些使用onRetainCustomNonConfigurationInstance来保存任意对象的代码(以一种奇特的方式,但是本质上是大对象,而没有引用Activity等).我从未见过使用过这种方法,所以我有些怀疑:

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.). I have never seen this method used, so I have some doubts:

  • 调用此方法存储任意对象是否安全(从某种意义上说,我可以确定它会被调用,并且不会很快被弃用/移除)?
  • 此方法与onRetainNonConfigurationInstance()有什么不同,后者也应返回Object,并且本质上应该类似地工作?
  • 由于某些原因,使用保留片段还是更好吗?
  • 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?
  • Is using retained fragment still better, for some reason?

作为奖励,我将感谢任何其他提示或解决方案来保存对象的状态,如AsyncTaskObservable,查看演示者并继续

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

推荐答案

此方法可安全调用以存储任意对象(在某种意义上 我可以确定它会被调用,而且不会 已过时/已删除)?

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()是一个相对较新的方法,不建议使用.我真的认为它不会很快消失,因为没有理由为了删除它而引入新的东西.您可以安全地使用它.

onRetainCustomNonConfigurationInstance() is a relatively new method and it is not deprecated. 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.

此方法与onRetainNonConfigurationInstance()有何不同, 哪个也应该返回Object,并且本质上应该类似地工作?

How is this method different from onRetainNonConfigurationInstance(), which also should return Object, and in essence should work similarly?

onRetainNonConfigurationInstance()总是返回内部NonConfigurationInstances类的实例,该实例具有保留的片段,加载程序等状态.您不能(也不应)更改此系统行为.这就是为什么该方法为final,并且您无法覆盖它的原因.

onRetainNonConfigurationInstance() always return an instance of inner NonConfigurationInstances class with retained fragments, loaders etc states. You cannot (and should not) change this system behavior. That's why the method is final and you cannot override it.

如果要保留自定义实例,则需要覆盖onRetainCustomNonConfigurationInstance()并从那里返回它.

If you want to retain your custom instance, you need to override onRetainCustomNonConfigurationInstance() and return it from there.

实际上,onRetainNonConfigurationInstance()调用onRetainCustomNonConfigurationInstance()并保留具有其他状态(例如保留的片段和加载程序)的重新调整的实例.

In fact, onRetainNonConfigurationInstance() calls onRetainCustomNonConfigurationInstance() and retains retuned instance with the other states like retained fragments and loaders.

由于某些原因,使用保留片段还是更好吗?

Is using retained fragment still better, for some reason?

这完全取决于您的用例和首选项.逻辑可能是这样的.如果您的活动仅控制片段,而其中没有其他特殊逻辑,则使用保留的片段会更容易.如果您的活动需要保留,则可以安全地使用onRetainCustomNonConfigurationInstance()方法.就目前而言,在这两种情况下,状态仍然会被老旧且已弃用的

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. As for now, in both cases the state still gets retained by good old and deprecated onRetainNonConfigurationInstance() method.

p.s.关于有关存储状态的奖励问题,我宁愿建议查看 onSaveInstanceState()方法.它旨在存储状态.

p.s. Regarding the bonus question about storing a state I would rather suggest to look at onSaveInstanceState() method. It was intended for storing states.

更新: AndroidX自2018年11月5日发布不建议使用该方法,并带有以下说明: onRetainCustomNonConfigurationInstance 已被弃用.使用 ViewModel 来存储需要在配置更改后幸存的对象.

Update: AndroidX release from November 5, 2018 deprecated the method with the following note: onRetainCustomNonConfigurationInstance has been deprecated. Use a ViewModel for storing objects that need to survive configuration changes.

这篇关于使用onRetainCustomNonConfigurationInstance保留跨配置更改的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆