0

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

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Pi Net
  • 69
  • 8
  • 1
    You're correct that you cannot save classes like `Button` because you don't control the source code and therefore cannot implement `Parcelable`. An alternative would be to save the underlying data that was originally used to generate your `Button`s so that you can reconstruct the buttons when state is restored. – stkent Dec 30 '18 at 16:04

1 Answers1

1

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()

Greg Moens
  • 1,705
  • 8
  • 15