Shared Preference in Android

Frequently used code samples and tutorials
Site Admin
Posts: 35
Joined: Tue Nov 18, 2014 2:39 pm

Shared Preference in Android

Postby vivekkalady » Thu Nov 27, 2014 5:34 pm

You can save and retrieve key, value pair data from Shared preferences.
SharedPreferences values will persist across user sessions. Data in shared preferences will be persistent even though user closes the application.
You can get values from Shared preferences using getSharedPreferences() method.
You also need an editor to edit and save the changes in shared preferences.
Use SharedPreferences to store data: booleans, floats, ints, longs, and strings.

  1. MODE_APPEND - This will append the new preferences with the already existing preferences
  2. MODE_ENABLE_WRITE_AHEAD_LOGGING - Database open flag. When it is set , it would enable write ahead logging by default
  3. MODE_MULTI_PROCESS - This method will check for modification of preferences even if the sharedpreference instance has already been loaded
  4. MODE_PRIVATE - By setting this mode , the file can only be accessed using calling application
  5. MODE_WORLD_READABLE - This mode allow other application to read the preferences
  6. MODE_WORLD_WRITEABLE - This mode allow other application to write the preferences

Create Shared Preference with mode

Code: Select all

SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", MODE_PRIVATE);
Editor editor = pref.edit();

editor.putBoolean("key_name1", true);           // Saving boolean - true/false
editor.putInt("key_name2", "int value");        // Saving integer
editor.putFloat("key_name3", "float value");    // Saving float
editor.putLong("key_name4", "long value");      // Saving long
editor.putString("key_name5", "string value");  // Saving string

// Save the changes in SharedPreferences
editor.commit(); // commit changes

pref.getBoolean("key_name1", null);         // getting boolean
pref.getInt("key_name2", 0);             // getting Integer
pref.getFloat("key_name3", null);           // getting Float
pref.getLong("key_name4", null);            // getting Long
pref.getString("key_name5", null);          // getting String

editor.remove("key_name3"); // will delete key key_name3
editor.remove("key_name4"); // will delete key key_name4

// Save the changes in SharedPreferences
editor.commit(); // commit changes

 editor.commit(); // commit changes

A simple Reusable Code is Given Below for easy usage.

usage :

Code: Select all

loadPreferences("Score")  will return "5" (String)

Code: Select all

   public SharedPreferences getPreferences() {
       return MainActivity.this.getSharedPreferences("MyPref", Context.MODE_PRIVATE);

    public void savePreferences(String key, String value) {
       SharedPreferences sharedPreferences = getPreferences();
       SharedPreferences.Editor editor = sharedPreferences.edit();
       editor.putString(key, value);
   public String loadPreferences(String key)
       try {
           SharedPreferences sharedPreferences = getPreferences();
           String strSavedMemo = sharedPreferences.getString(key, "");
           return strSavedMemo;
       } catch (NullPointerException nullPointerException)

           return null;

Return to “Tutorials & Examples”

Who is online

Users browsing this forum: No registered users and 0 guests