Shared Preferences

Shared Preferences allows activities and applications to keep simple and small amount of data, in the form of key-value pairs similar to a Map that will persist even when the user closes the application. Android stores Shared Preferences settings as XML file in Shared Preferences folder under DATA/data/{application package} directory. The DATA folder can be obtained by calling Environment.getDataDirectory().

Shared Preferences is application specific, i.e. the data is lost on performing one of the following options:

  • On uninstalling the application
  • On cleaning the application data

In order to use shared preferences, you have to call a method getSharedPreferences() that returns a Shared Preference instance pointing to the file that contains the values of preferences.

SharedPreferences sharePref = getSharedPreferences("SharePrefEx", MODE_PRIVATE);

The first parameter is the key and the second parameter is the MODE. Types of modes:

Mode Description
MODE_PUBLIC will make the file public which could be accessible by other applications in the device
MODE_PRIVATE keeps the files private and secure user's data.
MODE_APPEND used while reading the data from SP file.

You can add data into Share Preference, by SharedPreferences.Editor and you can get object of this class by calling sharePref.edit() and don't forget to commit() after adding anything, until you commit() data will not save, as shown below:

SharedPreferences.Editor edit = sharePref.edit();
edit.putString("name", "John");
edit.commit();

Here we make a class AppStore where you can use Share Preference easily. Please check below:

AppStore.java
package com.theitbulls.shareprefex;

import android.content.Context;
import android.content.SharedPreferences;

public class AppStore {
    public static String getString(Context mContext, String key) {
        SharedPreferences sharePref = getSharePref(mContext);
        return sharePref.getString(key, null);
    }

    public static void putString(Context mContext, String key, String value) {
        SharedPreferences.Editor editor = getSharePref(mContext).edit();
        editor.putString(key, value);
        editor.commit();
    }

    public static int getInt(Context mContext, String key) {
        SharedPreferences sharePref = getSharePref(mContext);
        return sharePref.getInt(key, -1);
    }

    public static void putInt(Context mContext, String key, int value) {
        SharedPreferences.Editor editor = getSharePref(mContext).edit();
        editor.putInt(key, value);
        editor.commit();
    }

    public static long getLong(Context mContext, String key) {
        SharedPreferences sharePref = getSharePref(mContext);
        return sharePref.getLong(key, -1);
    }

    public static void putLong(Context mContext, String key, long value) {
        SharedPreferences.Editor editor = getSharePref(mContext).edit();
        editor.putLong(key, value);
        editor.commit();
    }

    public static float getFloat(Context mContext, String key) {
        SharedPreferences sharePref = getSharePref(mContext);
        return sharePref.getFloat(key, -1);
    }

    public static void putFloat(Context mContext, String key, float value) {
        SharedPreferences.Editor editor = getSharePref(mContext).edit();
        editor.putFloat(key, value);
        editor.commit();
    }

    public static boolean getBoolean(Context mContext, String key) {
        SharedPreferences sharePref = getSharePref(mContext);
        return sharePref.getBoolean(key, false);
    }

    public static void putBoolean(Context mContext, String key, boolean value) {
        SharedPreferences.Editor editor = getSharePref(mContext).edit();
        editor.putBoolean(key, value);
        editor.commit();
    }

    private static SharedPreferences getSharePref(Context mContext) {
        return mContext.getSharedPreferences(AppStore.class.getName(), Context.MODE_PRIVATE);
    }
}