In this post I will explain to you how we can store data in Android applications using shared preference like we store data in session on the server. In my last post, I explained how we can store data in SQLite using the Android Room persistence library if you have not read yet, please read it.  In Android Shared preference is the way to store a small amount of data in key/value format. You can save String, int, float, Boolean in Shared preference.

Where we use Shared preference

We can use shared preference(SP) in our application where we need to save user login information once we save information we keep the user logged in our app even we reboot our device or relaunch our application.

We also use a shared preference to remembered user preference for our app, such as preferred setting for app.

How to create SP in Android?

To create a shared preference we need to create an Android application using an android studio. We use the android getSharedPreferences() method to GET/SET data from shared preference.

SharedPreferences share = getSharedPreferences("Prevefrence_Name", "PREFERENCE_MODE");

Shared preference has three modes in which we can save data. This method takes two arguments, first being the name of the SharedPreference file and the other is the context mode that we want to store our file in.

  • Context.MODE_PUBLIC will make the file public which could be accessible by other applications in the device
  • Context.MODE_PRIVATE keeps the files private and secure.
  • Context.MODE_APPEND is used while reading the data from the Shared Preference file.

SP also has two nested classes which help to put data and other help to check whenever the value is changed in shared preference.

  1. SharedPreferences.Editor: Interface used to write(edit) data in the SP file. Once editing has been done, one must commit() or apply() the changes made to the file.
  2. SharedPreferences.OnSharedPreferenceChangeListener(): Called when a shared preference is changed, added, or removed. This may be called even if a preference is set to its existing value. This callback will be run on your main thread.

Methods of Shared Preference

contains(String key)
This method is used to check whether the preferences contain a preference.
This method is used to create a new Editor for these preferences, through which you can make modifications to the data in the preferences and atomically commit those changes back to the SharedPreferences object.
getAll()Retrieve all values from the preferences.
getBoolean(String key, boolean defValue)Retrieve a boolean value from the preferences.
getFloat(String key, float defValue)Retrieve a float value from the preferences.
getInt(String key, int defValue)Retrieve an int value from the preferences.
getLong(String key, long defValue)Retrieve a long value from the preferences.
getString(String key, String defValue)Retrieve a String value from the preferences.
getStringSet(String key, Set defValues)
Retrieve a set of String values from the preferences.

(SharedPreferences.OnSharedPreferencechangeListener listener)

This method is used to registers a callback to be invoked when a change happens to a preference.

(SharedPreferences.OnSharedPreferencechangeListener listener)

This method is used to unregisters a previous callback.

Write data in shared preference

SharedPreferences share = getSharedPreferences("Data", Context.MODE_PRIVATE);
SharedPreferences.Editor edit = share.edit();
edit.putLong("user_id", 1);

Read data in shared preference

SharedPreferences share = getSharedPreferences("Data", Context.MODE_PRIVATE);
share.getLong("user_id", 0);
share.getString("token", "");

Hope you like this post please don’t forget to subscribe our youtube channel.