A smart phone is a dynamic environment for software to exist in and an application can be interrupted for any number of reasons. Even turning the handset on its side will cause an activity to reload in orientation sensitive programs.
Android provides SQLite for storing and retrieving data but this would a little heavy handed for storing an instance value or two and fortunately the activity class has built-in methods that we can override and use to store primitive name or value pairs.
Our recipes get a little more involved from here on and so we will not be able to include all the code in the given examples. We will assume that the reader is familiar with the subjects covered in the past few recipes and will be able to create applications with the necessary elements without recourse to the exact text. If not, have a quick look through the preceding recipes. We will not introduce any new subjects without fully explaining them.
Create a new application project and call it
StateSaver
.Include these elements within the
main.xml
layout file:An EditText
A Button
A TextView
Provide them with the following android:ids:
@+id/edit_text
@+id/button
@+id/text_view
Change the text in the boxes to match the following screenshot:
In this recipe we will create a simple application that 'remembers' a line of text that we enter when the activity is reloaded. To do this we override the activity's onSaveInstanceState()
and onRestoreInstanceState()
methods:
Declare the three UI elements that we just created as class-wide fields in the activity Java file, as follows:
public class StateSaver extends Activity { private EditText mEditText; private Button mButton; private TextView mTextView;
We also need a
String
constant:private static final String KEY = null;
Inside the
onCreate()
method and after thesetContentView()
statement associate these views with their Resource IDs:mEditText = (EditText) findViewById(R.id.edit_text); mButton = (Button) findViewById(R.id.button); mTextView = (TextView) findViewById(R.id.text_view);
Create a click listener for our button (also inside
onCreate()
):mButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { mTextView.setText(mEditText.getText().toString); } });
Now is a good time to import any libraries. Pressing Shift + Ctrl + O will cause Eclipse to offer you a choice between
android.view.View.OnClickListener
andandroid.content.DialogInterface.OnClickListener
. Make sure that you selectandroid.view.View
.Beneath the
onCreate()
method, add theonSaveInstanceState()
method:@Override public void onSaveInstanceState(Bundle state) { state.putString(KEY, mTextView.getText().toString()); super.onSaveInstanceState(state); }
Beneath this last method, include the
onRestoreInstanceState()
method:@Override public void onRestoreInstanceState(Bundle state) { super.onRestoreInstanceState(state); mTextView.setText(state.getString(KEY)); }
Run the project on a device or emulator. Enter some text into the EditText view and click on the button. Then restart the activity by exiting and restarting or by rotating the handset. When the activity begins afresh, the TextView restores to its remembered state.
The way these state saving methods work is really quite simple. When our application is dropped from memory, a Bundle of name/value pairs can be stored with the onSaveInstanceState()
method. This Bundle is then handed back when the activity restarts to both the onRestoreInstanceState()
and the onCreate()
methods. This is an important point as the Bundle is made available to both procedures and gives us a choice over where and how to handle activity restarts. This is because onRestoreInstanceState()
is not called until after onStart()
meaning we can apply any initialization that we may need before restoring our values.
The two methods introduced here are not the only way to ensure that a screen component's state is stored.
Internal memory can also be used to store other private data and details on how to do this can be found in the recipe Using internal storage for private data in Chapter 5, Data and Security.
The recipe Storing public data on external storage in Chapter 5, Data and Security demonstrates how to use SD cards to store data available from outside an application.