0

In an app I am developing, I have a custom view in which I need to save the instance state when either the screen configuration changes or the app is moved to the background. I implement onSaveInstanceState() and onRestoreInstanceState(Parcelable savedInstanceState) and in the case of screen configuration changes, the instance state is saved and restored perfectly. However, when the app is moved to the background, onSaveInstanceState() is not called. This means I have no saved instance state when the activity is brought to the foreground again. My question is then:

Why is onSaveInstanceState() not called when the app is moved to the background, and is it possible to make my view call onSaveInstanceState() when the app is moved to the background?

Zero
  • 1,864
  • 3
  • 21
  • 39
  • This is strange behavior, does your custom view have an id? I tested this out, and my view did get the onSaveInstanceState call. With "moved to background", do you mean using the home button? – Rolf ツ Oct 06 '14 at 10:31
  • My view does have an id. Indeed the problem does not occur when using the home button. It occurs when you press the back button to leave the activity. – Zero Oct 06 '14 at 10:34
  • *It occurs when you press the back button to leave the activity.* - using back means you're leaving the app for good(so the state of the application will not be stored) and not that you send the app to the background. You'll need a more persistent storage option(like preferences, database etc) to save data when using back. – user Oct 06 '14 at 10:42

2 Answers2

1

It occurs when you press the back button to leave the activity. – Zero

When using the Back button to leave the application the onSaveInstanceState method is not called by the Android system, this is expected system behavior. The back button is meant to stop your app (Read: activity).

The Back button finishes your Activity and there is no need to save app state. But if there is reason to to this then you should override the onStop method for example and save data to the file system there.

Sources:

http://developer.android.com/training/basics/activity-lifecycle/recreating.html (Recomended read)

There are a few scenarios in which your activity is destroyed due to normal app behavior, such as when the user presses the Back button or your activity signals its own destruction by calling finish().

Rolf ツ
  • 8,611
  • 6
  • 47
  • 72
  • I thought this would be the case. I guess I will have to think about what would be logical app behavior in my case. Thanks for the good answer and the link! :) – Zero Oct 06 '14 at 10:59
-3

As per my knowledge, when app enter into background, on Activity it calls onPause() method. So please try by moving your code to onPause() method.

Moses
  • 333
  • 1
  • 6
  • 19
  • Yes it does call onPause() in the activity, but I need to restore instance state in my custom view and a view doesn't have an onPause() method. I could try to store all instance variables of my custom view in the onPause() method in the activity, but that seems really bulky. I think it's always best practice to implement the instance state restoration at the appropriate level, so in this case the view level :) – Zero Oct 06 '14 at 10:41
  • This answer is incorrect, you should save any app state in the onSaveInstanceState method not in onPause. Secondly view states are saved automatically by the Android system, so the onPause method in the Activity should not be used. – Rolf ツ Oct 06 '14 at 10:47