Shared Preferences - Android

Shared Preferences - Android

What is Shared Preferences?

It is one of the concept of Data Persistence, just like Database, SharedPreference also is used to store the data, but not the large data. It allows us to store the data in the Key - Value pair. It can be thought of as the Dictionary.

SharedPreferences are suitable in different situations, such as when we want to store user's setting. For example; User has selected Dark theme, so when user opens the app next time it should show them Dark theme.

Other cases are when, we want to use that piece of data in different activities of an application.

Writing Data in Shared Preferences

// Storing data into SharedPreferences
SharedPreferences sharedPreferences = getSharedPreferences("MySharedPref",MODE_PRIVATE);

// Creating an Editor object to edit(write to the file)
SharedPreferences.Editor myEdit = sharedPreferences.edit();

// Storing the key and its value as the data fetched from edittext
myEdit.putString("name", name.getText().toString());
myEdit.putInt("age", Integer.parseInt(age.getText().toString()));

// Once the changes have been made,
// we need to commit to apply those changes made,
// otherwise, it will throw an error
myEdit.commit();

In order to create the SharedPreference the "getSharedPreferences()" method is used. this method contains 2 parameter, name of the SharedPreference and the mode you want to set.

There are 3 Modes in total:

  1. MODE_PUBLIC: When this mode is used, your file is public and can be used by other applications.

  2. MODE_PRIVATE: Your file is in private mode here and can't be accessed by other apps.

  3. MODE_APPEND: It is used when you want to read your data from Shared Preference.

SharedPreferences.Editor myEdit = sharedPreferences.edit();

// Storing the key and its value as the data fetched from edittext
myEdit.putString("name", name.getText().toString());
myEdit.putInt("age", Integer.parseInt(age.getText().toString()));

Since, we want to add data into the SP, the editor must be created. And you can put data into the editor as shown above.

Once, you have entered all the data, SP must be committed. In order to save the data.

Reading Data from the Shared Preference


// Retrieving the value using its keys the file name
// must be same in both saving and retrieving the data
SharedPreferences sh = getSharedPreferences("MySharedPref", MODE_APPEND);

// The value will be default as empty string because for
// the very first time when the app is opened, there is nothing to show
String s1 = sh.getString("name", "");
int a = sh.getInt("age", 0);

Similarly, Create the Shared Preference here too. But with the MODE_APPEND. Since we want to read the data.

sh.getString, sh.getInt respective methods are used to get the value.

First parameter in these methods is the "key" we provided while creating those data, and other parameter is default value. If the SP was unable to retrieve data with that ID, it will return the default value.