Posts tagged SharedPreferences
Android SharedPreferences
8A quick and easy way to store data that needs to be persistent across different user sessions, and accessible by your whole application and not just one activity, is by using the SharedPreferences class. This can be used for example to store your app’s settings.
To demonstrate we’ll create a small demo with a CheckBox and an EditText box in the layout. The state of these will be saved when the app goes out of focus and restored when resumed.
First up, the very simple layout with the CheckBox and EditText.
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<CheckBox
android:id="@+id/prefsdemo_checkbox"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="6pt"/>
<EditText
android:id="@+id/prefsdemo_edittext"
android:layout_width="250dp"
android:layout_height="wrap_content"/>
</LinearLayout>
In your activity, you will need objects to reference the EditText and Checkbox. You will also need one for SharedPreferences. Lastly you will need a string constant for the name of the SharedPreferences (your app can have more than one). I’ve called mine “SharedPrefsDemoPreferences” – you can choose something more meaningful. This constant is public so you can access it from elsewhere in your app. Put these in at the top of your activity class.
public static final String PREFS_NAME = "SharedPrefsDemoPreferences"; private SharedPreferences mPrefs; private EditText mEditText; private CheckBox mCheckBox;
Override the onCreate() method and assign references to your SharedPreferences, EditText and Checkbox objects. Call getSharedPreferences() with the string constant you just defined. mEditText and mCheckBox are defined in our main.xml layout and we get references to these using findViewById().
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mPrefs = getSharedPreferences(PREFS_NAME, 0);
mEditText = (EditText)findViewById(R.id.prefsdemo_edittext);
mCheckBox = (CheckBox)findViewById(R.id.prefsdemo_checkbox);
}
Next we will override onResume(). This is called when the activity is resumed after going out of focus, as well as after onCreate() is finished. The code we’ll put here will set the values for our CheckBox and EditText using the values from our SharedPreferences object. Before we do this, add these two new string constants beneath PREFS_NAME. These are the names of the data we’ll be storing.
public static final String PREF_STRING = "PrefString"; public static final String PREF_BOOL = "PrefBool";
The first parameter for SharedPreferences.getString() and SharedPreferences.getBoolean() is the string name of the data we want to get. We pass the constants we just defined. The second represents default values to use if the values do not exist (which as yet, they do not). We take the values from these calls and use them to set the values of our EditText and CheckBox.
@Override
protected void onResume() {
mEditText.setText(mPrefs.getString(PREF_STRING, "EditText"));
mCheckBox.setChecked(mPrefs.getBoolean(PREF_BOOL, false));
super.onResume();
}
Next up we’ll be overriding onPause(). It’s called when the activity goes out of focus or before the activity is about to be destroyed. Here we’ll want to update the SharedPrefences values.
@Override
protected void onPause() {
Editor e = mPrefs.edit();
e.putBoolean(PREF_BOOL, mCheckBox.isChecked());
e.putString(PREF_STRING, mEditText.getText().toString());
e.commit();
Toast.makeText(this, "Settings Saved.", Toast.LENGTH_SHORT).show();
super.onPause();
}
To explain. First we get the Editor object for our SharedPreferences, we use putBoolean() and putString() to put the values from the CheckBox and EditText into the object. Lastly we call commit() to save the new values.
We also give the user a toast notification to let them know it was saved.
Here’s the java class for this demo activity in full.
SharedPrefDemo.java
package samcoles.sharedprefsdemo;
import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
public class SharedPrefsDemo extends Activity {
public static final String PREFS_NAME = "SharedPrefsDemoPreferences";
public static final String PREF_STRING = "PrefString";
public static final String PREF_BOOL = "PrefBool";
private SharedPreferences mPrefs;
private EditText mEditText;
private CheckBox mCheckBox;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mPrefs = getSharedPreferences(PREFS_NAME, 0);
mEditText = (EditText)findViewById(R.id.prefsdemo_edittext);
mCheckBox = (CheckBox)findViewById(R.id.prefsdemo_checkbox);
}
@Override
protected void onResume() {
mEditText.setText(mPrefs.getString(PREF_STRING, "EditText"));
mCheckBox.setChecked(mPrefs.getBoolean(PREF_BOOL, false));
super.onResume();
}
@Override
protected void onPause() {
Editor e = mPrefs.edit();
e.putBoolean(PREF_BOOL, mCheckBox.isChecked());
e.putString(PREF_STRING, mEditText.getText().toString());
e.commit();
Toast.makeText(this, "Settings Saved.", Toast.LENGTH_SHORT).show();
super.onPause();
}
}
Try modifying the EditText and CheckBox, exiting and then coming back to the application later – the values will remain the same.