Search code examples
androidarraysobjectonsaveinstancestate

Saving Objects in onSaveInstanceState()


I have seen several questions similar to this about saving objects using Parceable (see here). But I want to save an array of an object which I cannot implement Parceable for because it is a predefined object which we get in android (Button, Color, etc...). How would I go about saving an array of these objects? I am really sorry if I am just missing something.

Thanks,

Pi Net


Solution

  • For non-parcelable data, you can override onRetainNonConfigurationInstance(). From there you can return any Object you like and that Object does not have to be parcelable. Then from your new instance of the activity, you can use getLastNonConfigurationInstance() to get access to the Object you saved earlier.

    https://developer.android.com/reference/android/app/Activity.html#onRetainNonConfigurationInstance()