Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I implement onRetainNonConfigurationInstance?

Tags:

android

state

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!

like image 905
Philipp Avatar asked Jan 18 '11 05:01

Philipp


1 Answers

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.

like image 92
Romain Guy Avatar answered Nov 08 '22 10:11

Romain Guy