Which one to use: onSaveInstanceState vs. onRetainNonConfigurationInstance?

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.

Leave a Comment