Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onSaveInstanceState() and onPause()

Tags:

android

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

like image 631
dataonly Avatar asked Mar 02 '11 10:03

dataonly


People also ask

What is the difference between the onPause () method and onSaveInstanceState () method?

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.

What is onSaveInstanceState () and onRestoreInstanceState () in activity?

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.

What is the difference between onPause and onStop?

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.

What is the use of onPause () method?

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.


2 Answers

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.

like image 152
Cosmin Avatar answered Oct 27 '22 13:10

Cosmin


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.

like image 45
Heiko Rupp Avatar answered Oct 27 '22 14:10

Heiko Rupp