How to use Android SharedPreferences


 SharedPreferences
 It store private primitive data in key-value pairs.

The SharedPreferences class provides a general framework that allows you to save and retrieve persistent key-value pairs of primitive data types. You can use SharedPreferences to save any primitive data: booleans, floats, ints, longs, and strings. This data will persist across user sessions (even if your application is killed).

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.
To get a SharedPreferences object for your application, use one of two methods:
  • getSharedPreferences() - Use this if you need multiple preferences files identified by name, which you specify with the first parameter.
  • getPreferences() - Use this if you need only one preferences file for your Activity. Because this will be the only preferences file for your Activity, you don't supply a name.
To write values:
  1. Call edit() to get a SharedPreferences.Editor.
  2. Add values with methods such as putBoolean() and putString().
  3. Commit the new values with commit()
To read values, use SharedPreferences methods such as getBoolean() and getString().

Available mode for shared preference:

   1. MODE_WORLD_READABLE
   2. MODE_WORLD_WRITEABLE
   3. MODE_PRIVATE
 
 

Store,Fetch,Remove,Clear Data from SharedPreferences.

 
/******* Create SharedPreferences *******/
 
    SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", MODE_PRIVATE);
    Editor editor = pref.edit();
 
 
/**************** Storing data as KEY/VALUE pair *******************/
 
    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
 
 
/**************** Get SharedPreferences data *******************/
 
// If value for key not exist then return second param value - In this case null
 
    pref.getBoolean("key_name1", null);         // getting boolean
    pref.getInt("key_name2", null);             // getting Integer
    pref.getFloat("key_name3", null);           // getting Float
    pref.getLong("key_name4", null);            // getting Long
    pref.getString("key_name5", null);          // getting String
 
 
 
 
/************ Deleting Key value from SharedPreferences *****************/
 
    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
    
/************ Clear all data from SharedPreferences *****************/  
 
     editor.clear();
     editor.commit(); // commit changes

 


  



Comments