Android Shared Preferences

Shared preferences are used to read and write small amounts of primitive data in the form of key/value pairs to a file on the device storage. The SharedPreference class provides APIs for getting a handle to a preference file and for reading, writing, and managing this data. The shared preferences are managed by the Android framework, and accessible to all the components of your app. But the data is not  shared with or accessible to any other apps.

The data you save to shared preferences is different from the data in the saved activity state.The data in the activity instance state is retained across activity instances in the same user session. Shared preferences persist across user sessions, even if your app is killed and restarted or if the device is rebooted.

Shared preferences are used only when you need to save a small amount of data as simple key/value pairs. To manage larger amounts of data means app data use  SQL databases, PHP MYSQl and other server side databases to  access data remotely.

Creating a shared preferences file:

You need only one shared preferences file for your app, and it is customarily named with the package name of your app as prefix. This makes its name unique and easily associated with your app.

private String mysharedPrefFile = "";
mPreferences = getSharedPreferences(mysharedPrefFile, MODE_PRIVATE);

Saving shared preferences:

You can save preferences in the onPause() state of the activity lifecycle using the SharedPreferences.Editor interface.

The  SharedPreferences.Editor performs all the file operations for you. When two editors are modifying preferences  at the same time, then the last modified one will be modified and saved.

Use  key/value pairs to add data with the editor using the put() method to store data. The put methods will overwrite previously existing values of an existing key.

We will use or call apply() method to save the preferences asynchronously,

The shared preferences editor also has a commit() method to synchronously save the preferences. but it  block’s other operations. As  SharedPreferences instances are singletons pattern within a process, it’s safe to replace any instance of  commit() with apply() if you were already ignoring the return value.

protected void onPause() {
SharedPreferences.Editor preferencesEditor = myPreferences.edit();
preferencesEditor.putInt("count", myCount);
preferencesEditor.putInt("color", myCurrentColor);

Restoring shared preferences:

myPreferences = getSharedPreferences(mysharedPrefFile, MODE_PRIVATE);
if (savedInstanceState != null) {
myCount = myPreferences.getInt("count", 1);
myShowCount.setText(String.format("%s", myCount));

myCurrentColor = myPreferences.getInt("color", myCurrentColor);
} else { … }

Clearing shared preferences:

To clear all the values in the shared preferences file, call the clear() method on the shared preferences editor and apply the changes then it will clears all the saved shared preferences data.

SharedPreferences.Editor preferencesEditor = myPreferences.edit();
preferencesEditor.putInt("number", 42);

Listening for preference changes:

public class MySettingsActivity extends PreferenceActivity
implements OnSharedPreferenceChangeListener {
public static final String KEY_PREF_SYNC_CONN = "pref_syncConnectionType";

public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
String key) {
if (key.equals(KEY_PREF_SYNC_CONN)) {
Preference myconnectionPref = findPreference(key);
// Set summary to be the user-description for the selected value
myconnectionPref.setSummary(sharedPreferences.getString(key, ""));


For proper lifecycle management in the activity, register and unregister your SharedPreferences.OnSharedPreferenceChangeListener during the onResume() and onPause() callback methods, respectively and will be executed during the activity life cycles methods and will be managed automaticaly.Below sample code used for register and unregister.

protected void onResume() {

protected void onPause() {

Hold a reference to the listener:

Below example code explains how to hold reference to the listener by using

prefs.registerOnSharedPreferenceChangeListener(listener); method

SharedPreferences.OnSharedPreferenceChangeListener listener =
new SharedPreferences.OnSharedPreferenceChangeListener() {
public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
// listener implementation



Congratulations You Have Learned About SharedPreferences

Leave a Reply