Shared Preferences in Android

04 / Aug / 2015 by Akhilesh Dubey 0 comments

Shared Preferences in Android

Android Prefereces provide a way to store user and application data in device. It stores primitive data including String objects in key-value pairs.

There are several ways to store data in android:

  1. Preferences(Shared and User):
    Store private primitive data in key-value pairs.
  2. Internal Storage:
    Store private data on the device memory.
  3. External Storage:
    Store public data on the shared external storage.
  4. SQLite database:
    Store structured data in a private database.
  5. Content Provider:
    Share data with other apps Shared Preferences

Shared Preferences(Permanent Storage):

  • Very simple way of share small amount of data between activities. Also for saving the state of the app. Preferences can stores only primitive types of data.
  • Preferences can be private or public (world readable, writeable)(deprecated from Api 17)
  • To get SharedPreferences, use
    – getSharedPreferences(String name) (if multiple pref. files)
    – getPreferences() (if only one pref. File is needed)
  • To write values, call edit() to get SharedPreferences.Editor to be used when adding values to file.


[code language=”java”]
public void savePreferences(){
SharedPreferences mySPref = getSharedPreferences(MYPREFS,0);
SharedPreferences.Editor editor = mySharedPreferences.edit();
editor.putBoolean("isTrue", true);
editor.putString("EntryValue", "Not Empty");

public void loadPreferences() {
SharedPreferences mySPref = getSharedPreferences(MYPREFS,0);
boolean isTrue = mySharedPreferences.getBoolean("isTrue", false);
String stringPref = mySharedPreferences.getString("EntryValue","");

What getSharedPreferences() does?

[code language=”java”]
public abstract SharedPreferences getSharedPreferences (String name, int mode)

  • returns: One instance of the SharedPrefrences.
  • name: Desired preferences file. If a preferences file by this name does not exist, it will be created when you retrieve an editor.
  • mode: Operating mode.
  • 0 or MODE_PRIVATE for the default operation.
    MODE_MULTI_PROCESS used if multiple processes are mutating the same SharedPreferences file.

The getSharedPreferences() is Context class’s method. Use this if you need multiple preferences files identified by name, which you specify with the first parameter.

The getPreferences(int MODE) method uses the getSharedPreferences() method with the name of the activity class for the preference file name. (for user prefrences). scope limited to activity where it is created.

So if you want the preference to be available in another activity or context, you have to use either of getSharedPreferences() or PreferenceManager.getDefaultSharedPreferences(context)

Listeners in SharedPreference:

SharedPreferences keeps listeners in a WeakHashMap. This means that you cannot use an anonymous inner class as a listener, as it will become the target of garbage collection as soon as you leave the current scope.

[code language=”java”]
public abstract void registerOnSharedPreferenceChangeListener (OnSharedPreferenceChangeListener listener)

public abstract void unregisterOnSharedPreferenceChangeListener (OnSharedPreferenceChangeListener listener)

commit() vs. apply()

  • apply() is asynchronous and commit() is synchronous.
  • apply() does not return boolean value and commit() returns.
  • commit() will block until all async commits are completed.

getSharedPreferences() vs getPreferences() vs getDefaultPreferences()

getSharedPreferences(String name, int mode) method of the Context class. The preferences are stored in a default file (1) or you can specify a file name (2) to be used to refer to the preferences.

User Preferences(private to Activity):

  • Shared preferences are not strictly for saving “user preferences,” such as what ringtone a user has chosen. If you’re interested in creating user preferences for your application, see PreferenceActivity, which provides an Activity framework for you to create user preferences, which will be automatically persisted (using shared preferences).
  • These preferences can only be used within the particular activity.
  • PreferenceActivity is the base class for an activity to show a hierarchy of preferences to the user.
  • It is generally used for saving the setting/preference of User

Steps to create user preference:

  • Create xml resource for Preference.
  • Create a Class which extends PreferenceActivity class , and inflate the created xml using addPreferencesFromResource() method.
  • Call/Start the PreferenceActivity (created in 2nd steps) from where you want.

PrefrenceActivity and PreferenceFragment:
PreferenceActivity is the base class for an activity to show a hierarchy of preferences to the user. Prior to HONEYCOMB this class only allowed the display of a single set of preference; this functionality should now be found in the new PreferenceFragment class.

Secure Coding in Android



Leave a Reply

Your email address will not be published. Required fields are marked *