简体   繁体   中英

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 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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