27

I'm a little bit confused about these two methods in Android.

From the documentation I understand that onSaveInstanceState() should be called to store only temporary information, and onPause() should be used to store any persistent data.

I was wondering why to call onSaveInstance() at all, when onPause() is called every time. Then I read in the Notepad tutorial:

"Note that saveState() must be called in both onSaveInstanceState() and onPause() to ensure that the data is saved. This is because there is no guarantee that onSaveInstanceState() will be called and because when it is called, it is called before onPause()."

There is no guarantee that onSaveInstanceState() will be called because you can simply walk out of the activity using the back button.

But according to this if you don't save the persistent data inside both methods, the app might be killed while inside onSaveInstanceState().

So we need to save the persistent data in both methods actually, am I right?

But if this is true, isn't this too much of an overhead and maybe there should be some other additional flag to tell if the method is already called or something?

http://developer.android.com/resources/tutorials/notepad/notepad-ex3.html

Micha Wiedenmann
  • 19,979
  • 21
  • 92
  • 137
dataonly
  • 669
  • 2
  • 7
  • 5

3 Answers3

20

From the developer guide on activities:

Note: Because onSaveInstanceState() is not guaranteed to be called, you should use it only to record the transient state of the activity (the state of the UI)—you should never use it to store persistent data. Instead, you should use onPause() to store persistent data (such as data that should be saved to a database) when the user leaves the activity.

Cosmin
  • 2,365
  • 2
  • 23
  • 29
  • 4
    `onSaveInstanceState() is not guaranteed to be called` now that's some crazy bullshit! why bother having it at all then ? – Someone Somewhere Aug 09 '18 at 15:53
  • @SomeoneSomewhere Maybe it was not true back in 2013? Currently from https://developer.android.com/reference/android/app/Activity.html#onSaveInstanceState(android.os.Bundle) I infer it is guaranteed - you're right now. – PrzemekTom Mar 13 '19 at 09:20
11

onSaveInstanceState() is meant to "remember" the current state when a configuration change occurs like e.g. a screen orientation change. This is not meant for "long term persistence".

In the notepad example this may be the same in both cases. In other applications, like e.g. a Twitter client, the data itself may be persisted in a background service. In onCreate() some items are pulled from the DB and displayed and e.g. the current position is remembered. When an orientation change occurs, this "current position" could be remembered in onSaveInstanceState() and later in onCreate() after the orientation change be used to display the message the user has last looked at.

See also this guide.

Heiko Rupp
  • 30,426
  • 13
  • 82
  • 119
4

You can save non-view instance state ("internal state such as user preferences") on a soft kill (orientation change) in onSaveInstanceState using bundles and on a hard kill (back button while we are in focus) in onStop using preferences. If you have other data ("shared document-like data -- typically stored in a SQLite database using a content provider"), you should do this in onPause.

Micha Wiedenmann
  • 19,979
  • 21
  • 92
  • 137
JAL
  • 3,319
  • 2
  • 20
  • 17