简体   繁体   English

我应该实现onRetainNonConfigurationInstance吗?

[英]Should I implement onRetainNonConfigurationInstance?

I just read about maintaining state of my android app and stumbled upon onRetainNonConfigurationInstance. 我刚刚阅读了关于维护我的Android应用程序的状态并偶然发现了onRetainNonConfigurationInstance。 But while reading the documentation I noticed this sentence: 但在阅读文档时,我注意到了这句话:

This function is called purely as an optimization, and you must not rely on it being called. 此函数纯粹称为优化,您不能依赖它被调用。

So I wonder: when does it really make sense to use this method. 所以我想知道:什么时候使用这种方法真的很有意义。 If I cannot rely on it being called, I need another mechanism to transfer state anyway (which in my case would be some serialization). 如果我不能依赖它被调用,我还需要另一种机制来传输状态(在我的情况下,这将是一些序列化)。 So unless I experience any performance hits, is there any reason why I should go for onRetainNonConfigurationInstance? 因此,除非我遇到任何性能命中,否则我有理由选择onRetainNonConfigurationInstance吗?

If it would be guaranteed to be called, I'd love to use it, but if it's not, it seems pretty useless. 如果保证可以使用它,我会喜欢使用它,但如果不是,它似乎没用。

Am I missing something? 我错过了什么吗? When do you use this method? 你什么时候使用这种方法? How would you keep a network connection or things like that? 你会如何保持网络连接或类似的东西?

Thanks! 谢谢!

What the documentation mean is that there are cases when the Activity will not be recreated immediately, in which case onRetainNonConfigurationInstance() will not be called. 文档的含义是,有些情况下不会立即重新创建Activity,在这种情况下,不会调用onRetainNonConfigurationInstance()。 You cannot use this method to persist data. 您无法使用此方法来保留数据。 It is only used to transfer objects that are expensive to create during a configuration change. 它仅用于传输在配置更改期间创建的昂贵对象。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM