Should I implement onRetainNonConfigurationInstance?

吃可爱长大的小学妹 提交于 2019-12-10 14:13:29

问题


I just read about maintaining state of my android app and stumbled upon 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?

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!


回答1:


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. You cannot use this method to persist data. It is only used to transfer objects that are expensive to create during a configuration change.



来源:https://stackoverflow.com/questions/4720927/should-i-implement-onretainnonconfigurationinstance

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!