nicholas.hauschild
nicholas.hauschild

Reputation: 42849

onRestoreInstanceState() not being called when it is expected

I have run into some trouble lately with onSaveInstanceState() and onRestoreInstanceState(). I have multiple activities that use these methods to save state in the case that they are cleaned up by the system. Most of them work, except for one, and I have no idea why.

What specifically happens is this:

I have noticed that the onSaveInstanceState() is called, onCreate() is called, but has a null bundle, and onRestoreInstanceState() is never called.

Has anyone seen this before?

Upvotes: 12

Views: 16282

Answers (3)

Ognyan
Ognyan

Reputation: 13600

The explanation why onRestoreInstanceState() is not called (or more peciselly: when it is called) is given in another question's thread: onSaveInstanceState () and onRestoreInstanceState ()

Upvotes: 1

nicholas.hauschild
nicholas.hauschild

Reputation: 42849

I believe I have figured out the issue, and it is with details that I did not disclose in the original question.

One of the things I am saving in my bundle is quite large (a 500x1000 pixel Bitmap). When I removed that from my Bundle, everything else was saved, the onCreate() method was called with the Bundle, and onRestoreInstanceState() was called as well.

Thus I believe there is a maximum size Bundle that you can save in onSaveInstanceState(), which is not documented. (at least as far as I can tell)

Upvotes: 4

CommonsWare
CommonsWare

Reputation: 1006869

The dev tools setting you are using may not have the behavior you expect.

If you want to test onSaveInstanceState()/onRestoreInstanceState(), the simplest thing to do is to change the orientation (<Ctrl>-<F11>). By default, your activity is destroyed and recreated, using the instance state.

Upvotes: 3

Related Questions