Android SharedPreferences

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Introduction

SharedPreferences provide a way to save data to disk in the form of key-value pairs.

Syntax

  • Context Method

    • public SharedPreferences getSharedPreferences(String name, int mode)
  • Activity Method

    • public SharedPreferences getPreferences()
  • SharedPreferences Methods

    • public SharedPreferences.Editor edit()
    • public boolean contains()
    • public Map<String, ?> getAll()
    • public boolean getBoolean(String key, boolean defValue)
    • public float getFloat(String key, float defValue)
    • public int getInt(String key, int defValue)
    • public long getLong(String key, long defValue)
    • public String getString(String key, String defValue)
    • public Set getStringSet(String key, Set defValues)
    • public void registerOnSharedPreferenceChangeListener (SharedPreferences.OnSharedPreferenceChangeListener listener)
    • public void unregisterOnSharedPreferenceChangeListener (SharedPreferences.OnSharedPreferenceChangeListener listener)
  • SharedPreferences.Editor Methods

    • public void apply()
    • public boolean commit()
    • public SharedPreferences.Editor clear()
    • public SharedPreferences.Editor putBoolean (String key, boolean value)
    • public SharedPreferences.Editor putFloat (String key, float value)
    • public SharedPreferences.Editor putInt (String key, int value)
    • public SharedPreferences.Editor putLong (String key, long value)
    • public SharedPreferences.Editor putString (String key, String value)
    • public SharedPreferences.Editor putStringSet (String key, Set values)
    • public SharedPreferences.Editor remove (String key)

Parameters

ParameterDetails
keyA non-null String identifying the parameter. It can contain whitespace or non-printables. This is only used inside your app (and in the XML file), so it doesn't have to be namespaced, but it's a good idea to have it as a constant in your source code. Don't localize it.
defValueAll the get functions take a default value, which is returned if the given key is not present in the SharedPreferences. It's not returned if the key is present but the value has the wrong type: in that case you get a ClassCastException.

Remarks

  • SharedPreferences shouldn't be used for storing large amount of data. For such purposes, it's much better to use SQLiteDatabase.

  • SharedPreferences are single process only, unless you use deprecated mode MODE_MULTI_PROCESS. So if your app has multiple processes, you won't be able to read main process's SharedPreferences in another process. In such cases, you should use another mechanism to share data across processes, but don't use MODE_MULTI_PROCESS as it is not reliable as well as deprecated.

  • It's better to use SharedPreferences instance in Singleton class to access all over the Application context. If you want to use it only for particular Activity go for getPreferences().

  • Avoid storing sensitive information in clear text while using SharedPreferences since it can be read easily.

Official Documentation

https://developer.android.com/reference/android/content/SharedPreferences.html



Got any Android Question?