12

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 my emulator running, and the dev tools setup to destroy() all activities when they are no longer visible. (So that I can prove that save/restore are working properly.)
  • The Activity in question is started up, and I proceed to change some of its state.
  • I start a new Activity, let it run for a moment, and then use the 'back' button to go to my previous Activity.
  • Upon arriving back at the Activity in question, I notice that the default state is back, thus, it does not have the state it should.

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?

gary
  • 4,227
  • 3
  • 31
  • 58
nicholas.hauschild
  • 42,483
  • 9
  • 127
  • 120
  • I'm sorry, I know this questions is for long time ago but how did you save the state of your activity? Would you please look at my question? http://stackoverflow.com/questions/26142255/retrieve-an-activity-after-time-out-warning-notification – Hamid Oct 01 '14 at 20:25
  • Please check my [solution](https://stackoverflow.com/a/62966140/2828651) to get it called. – صلي علي محمد - Atef Farouk Jul 18 '20 at 08:06

3 Answers3

4

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)

nicholas.hauschild
  • 42,483
  • 9
  • 127
  • 120
3

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.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Thank you for your answer. Something I didn't mention is that I have locked this Activity with the following attributes in the Manifest: android:configChanges="keyboardHidden|orientation" android:screenOrientation="portrait" – nicholas.hauschild Jun 28 '10 at 12:13
  • I tried changing orientation and that did called `onRestoreInstanceState()` method. But I don't restore variable in this method and still my EditText had previously entered values, instead of having nothing. Can you explain? – Geek Aug 13 '13 at 10:47
  • @Akash: There is a built-in implementation of `onSaveInstanceState()` and `onRestoreInstanceState()` that handle obviously user-mutable widget state, like the text in an `EditText`. – CommonsWare Aug 13 '13 at 10:58
  • @CommonsWare Ok. Then why do we need to override these methods when those are handled by SDK? – Geek Aug 13 '13 at 11:33
  • @Akash: You only need to override those methods if you have *other* state that is *not* represented in something that the built-in `onSaveInstanceState()` will handle. Not everything in an activity is necessarily text in an `EditText`, for example. – CommonsWare Aug 13 '13 at 11:36
  • @CommonsWare Ok. So lets say, I have activity where on button click I display a WebView, otherwise some other view is displayed. When app gets killed WebView was visible. So here should I save state, using flags, that WebView was displayed and display it on next invoke to `onRestoreInstanceState()`? – Geek Aug 13 '13 at 11:47
  • @Akash: I have no idea, as I am not the author of this activity and do not know what the expectations are. If you have further concerns in this area, ask a separate StackOverflow question, and explain **completely and precisely** what you are trying to do. – CommonsWare Aug 13 '13 at 11:49
1

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

Community
  • 1
  • 1
Ognyan
  • 13,452
  • 5
  • 64
  • 82