Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which one to use: onSaveInstanceState vs. onRetainNonConfigurationInstance?

Tags:

android

As far as I can see onRetainNonConfigurationInstance is a redundant callback. If my activity has really expensive initialization, I am better off using onSaveInstanceState. Saved instance covers more situations than non-configuration instance. Is there any guideline for using one API vs. the other? Thanks.

like image 258
Raj Avatar asked Nov 26 '10 13:11

Raj


1 Answers

As far as I can see onRetainNonConfigurationInstance is a redundant callback.

No, it is not.

If my activity has really expensive initialization, I am better off using onSaveInstanceState.

onSaveInstanceState() is not designed for "really expensive initialization". It is designed for "hey, the user made some changes to the information in the activity but has not saved it yet, let's not lose that data, m'kay?".

Is there any guideline for using one API vs. the other?

If it fits in a Bundle and is not too big, use onSaveInstanceState(). Everything that does not fit in a Bundle (e.g., a socket) or is really big (e.g., a photo as a Bitmap) should use onRetainNonConfigurationInstance(), and your application should be in position to re-create those items if needed.

like image 182
CommonsWare Avatar answered Oct 06 '22 04:10

CommonsWare