开发者

Should I implement onRetainNonConfigurationInstance?

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开发者_如何学JAVA 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!


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜