1

Till now, I was just putting everything, including non-view members, in the bundle in onSaveInstanceState() and retrieving it back in onRestoreInstanceState(). But I feel like I am not doing it the right way by persisting non-view data this way.

I have 6 arraylists of custom non-view objects in my app and all can grow considerably large. Serializing/deserializing them each time on screen rotation is causing a noticeable delay now. I was wondering if there's another way to do it that I am missing?

1 Answer 1

1

For configuration changes (e.g. rotation) there is an alternative solution. You can return an object in onRetainNonConfigurationInstance() (or, alternatively, use a Fragment with setRetainInstance(true)).

Unlike onSaveInstanceState(), this Object is not serialized, but just passed "at is", so there is no performance hit.

The onSaveInstanceState() / onCreate(Bundle savedInstanceState) mechanism is mostly intended for restoring the state when the app is killed by the system (e.g. due to low memory) and then restarted. In this case, a higher delay is justified.

Sign up to request clarification or add additional context in comments.

1 Comment

Agreed. I had to do the same thing when persisting a ton of bitmaps between config changes. Just created a fragment to store the bitmaps and had it retain its instance.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.