Symfony 2 Crash Course
Nice n' Easy JQuery Image Rotator
Enforce Coding Standards with PHP_CodeSniffer and Eclipse IDE on Ubuntu Linux
Tank Wars in G3D
Book Review: How to Implement Design Patterns in PHP
Development Resource Project

How to Resume an App and Restore State

Sunday, 9 October 11, 1:00 pm
When for whatever reason focus switches away from your app to another, for instance the user answers an incoming call or switches to the Home screen (which is itself an Activity just like any other), your app will be paused - that is, it will be suspended for an indefinite period. Whilst paused, it's quite possible that the system will kill it in order to free up its resources for whatever app is running in its place.

Before your app is suspended, the onSaveInstanceState() method of your Activity will be fired, followed by onPause(). You should place code in onSaveInstanceState() that saves any UI state that you don't want to lose. For instance, say your user is in the middle of filling out a form when the phone rings. You don't want the user to have to type in all the data again when they return to your app after the call, so you would save all the form values in onSaveInstanceState().

The method takes a Bundle parameter as its single argument, and you put values that you want to save into this Bundle. Basically you create as many name-value pairs that you need in the Bundle, and when your app resumes, this Bundle is used to restore the UI back to its original state, usually in the onCreate() method.
@Override public void onSaveInstanceState(Bundle savedInstanceState) { // Save UI state changes to the savedInstanceState. // This bundle will be passed to onCreate if the process is // killed and restarted. savedInstanceState.putBoolean("myBoolean", true); savedInstanceState.putDouble("myDouble", 1.9); savedInstanceState.putInt("myInt", 1); savedInstanceState.putString("myString", "Welcome back to Android"); // etc. super.onSaveInstanceState(savedInstanceState); }
Other data, that does not relate to UI state, should be saved in onPause(). Here you should save data to your own storage mechanisms, such as a SQLite database or a file.

Restoring the app's UI state once it gets focus again is usually handled by onCreate(), although occasionally you might wish to do it later once all the other initialisation tasks are complete by overriding onRestoreInstanceState(). In either case, you get the information you previously saved in onSaveInstanceState() from the Bundle argument both of these methods receive.
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Restore UI state from the savedInstanceState. boolean myBoolean = savedInstanceState.getBoolean("myBoolean"); double myDouble = savedInstanceState.getDouble("myDouble"); int myInt = savedInstanceState.getInt("myInt"); String myString = savedInstanceState.getString("myString"); // then all the other init tasks follow.. }

Please enter your comment in the box below. Comments will be moderated before going live. Thanks for your feedback!

Cancel Post

/xkcd/ Metric Tip