3

I am refering to http://developer.android.com/reference/android/app/Activity.html.

I have an activity which can be "interrupted" by the user, e.g. the user opens the menu to call the preferences screen. When calling the preference screen onSaveInstanceState(Bundle) is called and I can save my data. That's fine so far. But upon pressing the back button onRestoreInstanceState(Bundle savedInstanceState) is NOT called.

So how can I save my state? Do I have to do it when calling the new activity? But how?
The only way I can think of, is saving the state by passing the state to the new activity, do nothing there with the saved data, return it to the first activity and restore the state in onActivitResult. But that would mean that I have to pass data back and forth just to restore the state. Doesn't seem to be a nice solution.

Volo
  • 28,673
  • 12
  • 97
  • 125
AndyAndroid
  • 4,039
  • 14
  • 44
  • 71

3 Answers3

6

Probably a bad answer, but are you sure onRestoreInstanceState needs to be called?

The point of the bundle and onSaveInstanceState / onCreate with bundle / onRestoreInstanceState is to save transient data for an activity that is in the history stack, in case the activity must be killed to reclaim some memory. If it is killed, the activity can be restored, as if it were never killed, via onCreate / onRestonreInstanceState. However, if the activity was not killed, there may be no need to restore its transient data - presumably it is just as it was.

The Android documentation is careful to point out that onSaveInstanceStae / onRestoreInstanceState are not lifecycle methods, so aren't guaranteed to be called during lifecycle state transitions. If you need to hook into certain lifecycle transitions, take a look at the lifecycle hook methods. For example, onResume is called when the activity becomes the foreground activity and onPause is called when it is no longer the foreground activity.

Android Lifecycle Picture

Bert F
  • 85,407
  • 12
  • 106
  • 123
  • see comment above. I wanted to use the bundles, implemnting saving the data manually I wanted avoid. The first activity is a listview, after going back from the preference activity the list view is empty. Is there something special about listview why it is empty after switching the activities? – AndyAndroid Jan 28 '11 at 19:59
  • I just tested some other apps, and there after returning from the prefs screen all inputs still there what could be the reason for this? – AndyAndroid Jan 28 '11 at 20:19
  • 1
    @AndyAndroid - I played around with 1 of my apps & added `onStart()`, `onRestart()`, etc. methods to log the lifecycle (`onCreate()` is the only lifecycle method that I had before that). I saw my activities flip through the various lifecycle methods and soemtimes called `onSaveInstanceState`. However, I couldn't get the app to invoke `onRestoreInstanceState()` and every time I returned to me list, it was fine, i.e. the activity was not killed and maintained state. I set dev tools to destory every activity and it started calling `onRestoreInstanceState` too. – Bert F Jan 28 '11 at 22:16
4

Have a look at the Application Fundamentals, specifically this part:

Unlike onPause() and the other methods discussed earlier, onSaveInstanceState() and onRestoreInstanceState() are not lifecycle methods. They are not always called. For example, Android calls onSaveInstanceState() before the activity becomes vulnerable to being destroyed by the system, but does not bother calling it when the instance is actually being destroyed by a user action (such as pressing the BACK key). In that case, the user won't expect to return to the activity, so there's no reason to save its state.

Because onSaveInstanceState() is not always called, you should use it only to record the transient state of the activity, not to store persistent data. Use onPause() for that purpose instead.

Basically, any persistant data should be written out in your onPause() method and read back in onResume(). Check out the Data Storage article for ways of saving data.

Community
  • 1
  • 1
dave.c
  • 10,910
  • 5
  • 39
  • 62
  • using data storage I wanted to avoid. The onSaveInstanceState and onRestore use this bundle which makes it very convenient. So I have to do all the work manually... – AndyAndroid Jan 28 '11 at 19:57
1

Your ListView should not be cleared after returning from the Pref screen unless the Activity has been destroyed while the Pref screen was showing (in which case onCreate should have been called with the saved bundle).

The savedInstanceState is only used when the Activity has been destroyed and needs to be recreated. In this case, it looks like your ListActivity has not been destroyed.

Are you clearing your ListView manually somewhere?

gngr44
  • 1,368
  • 9
  • 10