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 bothonSaveInstanceState()
andonPause()
to ensure that the data is saved. This is because there is no guarantee thatonSaveInstanceState()
will be called and because when it is called, it is called beforeonPause()
."
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
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.
The onSaveInstanceState() method allows you to add key/value pairs to the outState of the app. Then the onRestoreInstanceState() method will allow you to retrieve the value and set it back to the variable from which it was originally collected.
If screen times out on your activity, then onPause is called. After sometime if you will not open the screen then onStop will be called.
onPause(): This method gets called when the UI is partially visible to the user. If a dialog is opened on the activity then the activity goes to pause state and calls onPause() method. Here the activity is in the paused state.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With