Saving the ToDo List Activity State

Currently, each time the To-Do List example application is restarted, all the to-do items are lost and any text entered into the text entry box is cleared. In this example you'll start to save the application state of the To-Do List application across sessions.

The instance state in the ToDoList Activity consists of three variables:

> Is a new item being added?

> What text exists in the new item entry textbox?

> What is the currently selected item?

Using the Activity's default Shared Preference you can store each of these values and update the UI when the Activity is restarted.

Later in this chapter you'll learn how to use the SQLite database to persist the to-do items as well. This example is a first step that shows how to ensure a seamless experience by saving Activity instance details.

1. Start by adding static string variables to use as preference keys:

private static final String TEXT_ENTRY_KEY = "TEXT_ENTRY_KEY"; private static final String ADDING_ITEM_KEY = "ADDING_ITEM_KEY"; private static final String SELECTED_INDEX_KEY = "SELECTED_INDEX_KEY"

2. Next, override the onPause method. Get the Activity's private Shared Preference object and its Editor object.

Using the keys you created in Step 1, store the instance values according to whether a new item is being added, and also store any text in the "new item'' edit box:

©Override protected void onPause(){ super.onPause();

// Get the activity preferences object. SharedPreferences uiState = getPreferences(O); // Get the preferences editor.

SharedPreferences.Editor editor = uiState.edit(); // Add the UI state preference values.

editor.putString(TEXT_ENTRY_KEY, myEditText.getText().toString()); editor.putBoolean(ADDING_ITEM_KEY, addingNew); // Commit the preferences. editor.commit();

3. Write a restoreUIState method that applies the instance values you recorded in the previous step when the application restarts. Modify the onCreate method to add a call to the restoreUIState method at the very end:

©Override public void onCreate(Bundle savedInstanceState) { [ ... existing onCreate logic ... ] restoreUIState();

private void restoreUIState() {

// Get the activity preferences object.

SharedPreferences settings = getPreferences(Activity.MODE_PRIVATE);

// Read the UI state values, specifying default values. String text = settings.getString(TEXT_ENTRY_KEY, ""); Boolean adding = settings.getBoolean(ADDING_ITEM_KEY, false);

// Restore the UI to the previous state. if (adding) { addNewItem(); myEditText.setText(text);

4. Record the index of the selected item using the onSaveInstanceState/onRestoreInstance State mechanism. It's then saved and applied only if the application is killed without the user's explicit instruction:

©Override public void onSaveInstanceState(Bundle savedInstanceState) {

savedInstanceState.putInt(SELECTED_INDEX_KEY, myListView.getSelectedItemPosition());

super.onSaveInstanceState(saveInstanceState);

©Override public void onRestoreInstanceState(Bundle savedInstanceState) { int pos = -1;

if (savedInstanceState != null)

if (savedInstanceState.containsKey(SELECTED_INDEX_KEY)) pos = savedInstanceState.getInt(SELECTED_INDEX_KEY, -1);

myListView.setSelection(pos);

All code snippets in this example are part of the Chapter 6 Todo List project, available for download at Wrox.com.

When you run the To-Do List application you should now see the UI state persisted across sessions. That said, the application still won't persist the To-Do List items — you'll add this essential piece of functionality in the next chapter.

Mobile Apps Made Easy

Mobile Apps Made Easy

Quick start guide to skyrocket your offline and online business success with mobile apps. If you know anything about mobile devices, you’ve probably heard that famous phrase coined by one of the mobile device’s most prolific creators proclaiming that there’s an app for pretty much everything.

Get My Free Training Guide


Post a comment