Signup/Sign In

Shared Preferences

Shared Preferences is a type of data storage used in Android to store private data in key-value pairs. It only allows you to store and retrieve primitive data types - boolean, float, int, long, and strings. The data you want to save gets stored in a XML file which can be found in the following directory: data/data/<package-name>/shared-prefs/yourFilename.xml


How to access SharedPreferences object

You need a SharedPreferences object to store and retrieve the key-value pairs of primitive data types. To get a SharedPreferences object for your application, you can use one of the following two methods:

  1. getSharedPreferences(String filename , int mode)

    Use this method if you need multiple preferences files to store your key-value pairs. To uniquely identify your file, you must give a unique name of the file, which you can specify with the first parameter.

  2. getPreferences(int mode)

    Use this method if you need only one preferences file to store key-value pairs. Since this will be the only single preferences file for your Activity, you don't need a name for the file. The mode defines the way in which you want to store your key-value pair data. Following are some type of modes:

  • MODE_PRIVATE

    This mode allows the SharedPreferences file to be accessed only by your application.

  • Context.MODE_MULTI_PROCESS

    This mode allows multiple processes to modify the same SharedPreferences file.

  • Context.MODE_APPEND

    This mode will append the new preferences added with the already existing preferences.

  • Note: Following modes were earlier used, but now they have been deprecated.

  • MODE_WORLD_READABLE

    This mode allowed other application to read the preferences.

  • MODE_WORLD_WRITEABLE

    This mode allowed other application to write the preferences.


Saving the Data

  1. Create a suitable type of SharedPreferences object.
  2. Call edit() method to get a SharedPreferences.Editor instance.
  3. Add key-value pair with methods such as putBoolean(key,value) , putString(key,value) etc.
  4. Commit the new values with commit() method.

When you want to store the data, for example, using putString() method, there are two kind of parameters used:

  • Key: You need to define a unique key that you will be needed when you want to retrieve the value related with that key.
  • Value: This is your data that you want to store.

For example:

SharedPreferences s = getSharedPreferences("myFile",Context.MODE_PRIVATE);
SharedPreferences.Editor e = s.edit();
e.putString("key1","I LOVE");
e.putString("key2","STUDYTONIGHT");
e.commit();

Retrieving the Data

  1. Create the SharedPreferences object of the same type that you have used while saving the data.

    Note: If you have used getSharedPreferences() method, then you need to provide the filename from which you want to retrieve your data.

  2. Use SharedPreferences methods such as getBoolean(key, default_value), getString(key, default value) etc. to get the value.

When you want to retrieve your data, for example, using getString() method, there are two kind of parameters used:

  • Key: You need to specify the same key that you have mentioned while saving the data so that you get the unique value (i.e. your data).
  • Default: If in the SharedPreferences file, the key you specified doesn’t match with any of the keys saved, then this particular default value gets displayed.

For example:

SharedPreferences ss = getSharedPreferences("myFile",Context.MODE_PRIVATE);
String message1 = ss.getString("key1","Key doesn’t match");
String message2 = ss.getString("key2","Key doesn’t match");