68

In an application I have been building we rely on SharedPreferences quite a bit, this got me thinking about what is best practice when it comes to accessing SharedPreferences. For instance many people say the appropriate way to access it is via this call:

PreferenceManager.getDefaultSharedPreferences(Context context)

However it seems like this could be dangerous. If you have a large application that is relying on SharedPreferences you could have key duplication, especially in the case of using some third party library that relies on SharedPreferences as well. It seems to me that the better call to use would be:

Context.getSharedPreferences(String name, int mode)

This way if you have a class that heavily relies on SharedPreferences you can create a preference file that is used only by your class. You could use the fully qualified name of the class to ensure that the file will most likely not be duplicated by someone else.

Also based on this SO question: Should accessing SharedPreferences be done off the UI Thread?, it seems that accesses SharedPreferences should be done off the UI thread which makes sense.

Are there any other best practices Android developers should be aware of when using SharedPreferences in their applications?

Community
  • 1
  • 1
Kam Sheffield
  • 1,231
  • 1
  • 14
  • 16

5 Answers5

104

I've wrote a little article that can also be found here. It describes what SharedPreferences is :

Best Practice: SharedPreferences

Android provides many ways of storing application data. One of those ways leads us to the SharedPreferences object which is used to store private primitive data in key-value pairs.

All logic are based only on three simple classes:

SharedPreferences

SharedPreferences is main of them. It's responsible for getting (parsing) stored data, provides interface for getting Editor object and interfaces for adding and removing OnSharedPreferenceChangeListener

  • To create SharedPreferences you will need Context object (can be an application Context)
  • getSharedPreferences method parses Preference file and creates Map object for it
  • You can create it in few modes provided by Context. You should always use MODE_PRIVATE, as all the other modes are deprecated since API level 17.

    // parse Preference file
    SharedPreferences preferences = context.getSharedPreferences("com.example.app", Context.MODE_PRIVATE);
    
    // get values from Map
    preferences.getBoolean("key", defaultValue)
    preferences.get..("key", defaultValue)
    
    // you can get all Map but be careful you must not modify the collection returned by this
    // method, or alter any of its contents.
    Map<String, ?> all = preferences.getAll();
    
    // get Editor object
    SharedPreferences.Editor editor = preferences.edit();
    
    //add on Change Listener
    preferences.registerOnSharedPreferenceChangeListener(mListener);
    
    //remove on Change Listener
    preferences.unregisterOnSharedPreferenceChangeListener(mListener);
    
    // listener example
    SharedPreferences.OnSharedPreferenceChangeListener mOnSharedPreferenceChangeListener
        = new SharedPreferences.OnSharedPreferenceChangeListener() {
      @Override
      public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
      }
    };
    

Editor

SharedPreferences.Editor is an Interface used for modifying values in a SharedPreferences object. All changes you make in an editor are batched, and not copied back to the original SharedPreferences until you call commit() or apply()

  • Use simple interface to put values in Editor
  • Save values synchronous with commit() or asynchronous with apply which is faster. In fact of using different threads using commit() is safer. Thats why I prefer to use commit().
  • Remove single value with remove() or clear all values with clear()

    // get Editor object
    SharedPreferences.Editor editor = preferences.edit();
    
    // put values in editor
    editor.putBoolean("key", value);
    editor.put..("key", value);
    
    // remove single value by key
    editor.remove("key");
    
    // remove all values
    editor.clear();
    
    // commit your putted values to the SharedPreferences object synchronously
    // returns true if success
    boolean result = editor.commit();
    
    // do the same as commit() but asynchronously (faster but not safely)
    // returns nothing
    editor.apply();
    

Performance & Tips

  • SharedPreferences is a Singleton object so you can easily get as many references as you want, it opens file only when you call getSharedPreferences first time, or create only one reference for it.

    // There are 1000 String values in preferences
    
    SharedPreferences first = context.getSharedPreferences("com.example.app", Context.MODE_PRIVATE);
    // call time = 4 milliseconds
    
    SharedPreferences second = context.getSharedPreferences("com.example.app", Context.MODE_PRIVATE);
    // call time = 0 milliseconds
    
    SharedPreferences third = context.getSharedPreferences("com.example.app", Context.MODE_PRIVATE);
    // call time = 0 milliseconds
    
  • As SharedPreferences is a Singleton object you can change any of It's instances and not be scared that their data will be different

    first.edit().putInt("key",15).commit();
    
    int firstValue = first.getInt("key",0)); // firstValue is 15
    int secondValue = second.getInt("key",0)); // secondValue is also 15
    
  • Remember the larger the Preference object is the longer get, commit, apply, remove and clear operations will be. So it's highly recommended to separate your data in different small objects.

  • Your Preferences will not be removed after Application update. So there are cases when you need to create some migration scheme. For example you have Application that parse local JSON in start of application, to do this only after first start you decided to save boolean flag wasLocalDataLoaded. After some time you updated that JSON and released new application version. Users will update their applications but they will not load new JSON because they already done it in first application version.

    public class MigrationManager {
     private final static String KEY_PREFERENCES_VERSION = "key_preferences_version";
     private final static int PREFERENCES_VERSION = 2;
    
     public static void migrate(Context context) {
         SharedPreferences preferences = context.getSharedPreferences("pref", Context.MODE_PRIVATE);
         checkPreferences(preferences);
     }
    
     private static void checkPreferences(SharedPreferences thePreferences) {
         final double oldVersion = thePreferences.getInt(KEY_PREFERENCES_VERSION, 1);
    
         if (oldVersion < PREFERENCES_VERSION) {
             final SharedPreferences.Editor edit = thePreferences.edit();
             edit.clear();
             edit.putInt(KEY_PREFERENCES_VERSION, currentVersion);
             edit.commit();
         }
     }
    }
    
  • SharedPreferences are stored in an xml file in the app data folder

    // yours preferences
    /data/data/YOUR_PACKAGE_NAME/shared_prefs/YOUR_PREFS_NAME.xml
    
    // default preferences
    /data/data/YOUR_PACKAGE_NAME/shared_prefs/YOUR_PACKAGE_NAME_preferences.xml
    

Android guide.

Sample Code

public class PreferencesManager {

    private static final String PREF_NAME = "com.example.app.PREF_NAME";
    private static final String KEY_VALUE = "com.example.app.KEY_VALUE";

    private static PreferencesManager sInstance;
    private final SharedPreferences mPref;

    private PreferencesManager(Context context) {
        mPref = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
    }

    public static synchronized void initializeInstance(Context context) {
        if (sInstance == null) {
            sInstance = new PreferencesManager(context);
        }
    }

    public static synchronized PreferencesManager getInstance() {
        if (sInstance == null) {
            throw new IllegalStateException(PreferencesManager.class.getSimpleName() +
                    " is not initialized, call initializeInstance(..) method first.");
        }
        return sInstance;
    }

    public void setValue(long value) {
        mPref.edit()
                .putLong(KEY_VALUE, value)
                .commit();
    }

    public long getValue() {
        return mPref.getLong(KEY_VALUE, 0);
    }

    public void remove(String key) {
        mPref.edit()
                .remove(key)
                .commit();
    }

    public boolean clear() {
        return mPref.edit()
                .clear()
                .commit();
    }
}
Community
  • 1
  • 1
Yakiv Mospan
  • 8,174
  • 3
  • 31
  • 35
  • what do you mean by _Remember the larger the Preference object is the longer get, commit, apply, remove and clear operations will be. So it's highly recommended to separate your data in different small objects._ small separate peferences objects? – Semanticer Jun 01 '16 at 09:23
  • 3
    @Semanticer preferences is a file, large it is - longer it takes to do all file operations. Separate preference files. Don't hold all data in one ` context.getSharedPreferences("com.example.app", Context.MODE_PRIVATE);`. Actually you can try and compare it for your case. Maybe one file will not affect performance for you. But still it is always better to separate logic on small understandable peaces – Yakiv Mospan Jun 02 '16 at 06:30
  • This answer should be marked. You earn my upvote sir. – Jakub S. Aug 19 '16 at 11:06
  • @YakivMospan, you said "In fact of using different threads using commit() is safer. Thats why I prefer to use commit().” So if you’re going to be saving to `SharedPreferences` from different `Threads` we should use `commit()` instead of `apply()`? Is it safer to use `commit()`? Will it ensure the value is updated correctly if the other thread tries to get it after? – Sakiboy Aug 03 '17 at 23:47
  • @Sakiboy from documentation https://developer.android.com/reference/android/content/SharedPreferences.Editor.html#apply() : "As SharedPreferences instances are singletons within a process, it's safe to replace any instance of commit() with `apply()` if you were already ignoring the return value." I wrote this post and article some time ago and probably what I mean that it is safer, cuz you don't know if `apply()` was finished successful or not. And in case of failure you changes will be not saved to disk. – Yakiv Mospan Aug 04 '17 at 07:59
  • @Sakiboy But in documentation, they told us that we can replace every `commit()` with `apply()` if we are not handling return value (success or failure). For different threads, again from docs, the last called `commit()` or `apply()` will be treated as valid value, no matter what thread you are using within one process, all `commit()` and `apply()` calls are queued and only last one will be applied. – Yakiv Mospan Aug 04 '17 at 07:59
  • @YakivMospan Is it fine to write the data in `SharedPreferences` every second. I need to do in one of my apps, there is going to be just one field. So, will that take up lot of memory? Should I opt for saving data every 5 seconds. – CopsOnRoad Dec 16 '18 at 17:33
  • Particularly useful the location of the XML files, which you can view in AndroidStudio from the menu View -> ToolWindows -> DeviceFileExplorer. – Davide Apr 30 '20 at 15:45
  • could you please update the article link? Its not working. – Arpit Patel Oct 08 '20 at 16:27
46

If you have a large application that is relying on SharedPreferences you could have key duplication, especially in the case of using some third party library that relies on SharedPreferences as well.

Libraries should not use that particular SharedPreferences. The default SharedPreferences should only be used by the application.

This way if you have a class that heavily relies on SharedPreferences you can create a preference file that is used only by your class.

You are certainly welcome to do this. I wouldn't, at the application level, as the primary reason for SharedPreferences is to have them be shared among the components in the application. A development team should have no problem managing this namespace, just as they should have no problem managing names of classes, packages, resources, or other project-level stuff. Moreover, the default SharedPreferences are what your PreferenceActivity will use.

However, going back to your libraries point, reusable libraries should use a separate SharedPreferences for their library only. I would not base it on a class name, because then you are one refactoring away from breaking your app. Instead, pick a name that is unique (e.g., based on the library name, such as "com.commonsware.cwac.wakeful.WakefulIntentService") but stable.

it seems that accesses SharedPreferences should be done off the UI thread which makes sense.

Ideally, yes. I recently released a SharedPreferencesLoader that helps with this.

Are there any other best practices Android developers should be aware of when using SharedPreferences in their applications?

Don't over-rely upon them. They are stored in XML files and are not transactional. A database should be your primary data store, particularly for data you really don't want to lose.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • This may seem amatuer but nonetheless would like to ask for lack of resource on the net. How does android handle shared preferences i.e is the Preferences file for an application loaded once or every call to the SharedPreferences is a File i/o. – humblerookie Aug 19 '14 at 07:20
  • 3
    @root: `SharedPreferences` are cached. The first access of a particular `SharedPreferences` loads the XML; subsequent reads will work off the cache. – CommonsWare Aug 19 '14 at 11:52
  • @CommonsWare I wonder what is the best way to store keys and default values. I can ask for specific shared preference from code and supply default value (then it would be String constant in java class) but I can also define default value in xml definition of preference (than is string in strings.xml). Same for key (can store it in java class and xml). My goal is to avoid writing it in both places but I need it. Somehow it feels too unsecure to store it in strings_preferences.xml – Ewoks Dec 03 '15 at 13:34
  • If I'm writing a library, what should I use instead of `SharedPreferences` for storing several simple key-val pairs? In this line, "Libraries should not use that particular SharedPreferences." do you mean that `SharedPreferences settings = getSharedPreferences("my_lib_pref_name", 0);` is bad practice because `my_lib_pref_name` may conflicts application's preference names? – Weishi Z Feb 02 '17 at 00:11
  • @WeishiZeng: No, what you propose is a good practice. I was referring to `PreferenceManager.getDefaultSharedPreferences()` as being the wrong `SharedPreferences` to use. I apologize for any confusion. – CommonsWare Feb 02 '17 at 00:21
  • @CommonsWare Thanks a ton! – Weishi Z Feb 02 '17 at 00:48
  • One of my colleagues is using a cache which will be populated with an entry everytime an entry is accessed from shared preferences. This cache is part of a helper class for shared preferences to store and retrieve key value pairs. IMO, since shared preferences already maintains a cache, this additional cache is not required which will keep the code in the helper class simple. 1 argument 4 cache approach is that accessing shared preferences API (framework class) can be avoided with internal cache at app code level. Not invoking framework API gives performance improvement. Your thoughts please. – garnet Aug 07 '18 at 18:22
  • @garnet: I can't really answer that. You are correct that the additional caching is not necessary. Avoiding the dependency on the framework class is often handled by an interface with multiple implementations, or by a concrete implementation plus mocks used in testing. – CommonsWare Aug 07 '18 at 22:12
  • Checkout this for easy implementation https://arkapp.medium.com/how-to-use-shared-preferences-the-easy-and-fastest-way-98ce2013bf51 – abdul rehman May 31 '21 at 06:56
7

In kotlin, use of SharedPreferences can be simplified in the following way.

class Prefs(context: Context) {

    companion object {
        private const val PREFS_FILENAME = "app_prefs"

        private const val KEY_MY_STRING = "my_string"
        private const val KEY_MY_BOOLEAN = "my_boolean"
        private const val KEY_MY_ARRAY = "string_array"
    }

    private val sharedPrefs: SharedPreferences =
        context.getSharedPreferences(PREFS_FILENAME, Context.MODE_PRIVATE)

    var myString: String
        get() = sharedPrefs.getString(KEY_MY_STRING, "") ?: ""
        set(value) = sharedPrefs.edit { putString(KEY_MY_STRING, value) }

    var myBoolean: Boolean
        get() = sharedPrefs.getBoolean(KEY_MY_BOOLEAN, false)
        set(value) = sharedPrefs.edit { putBoolean(KEY_MY_BOOLEAN, value) }

    var myStringArray: Array<String>
        get() = sharedPrefs.getStringSet(KEY_MY_ARRAY, emptySet())?.toTypedArray()
            ?: emptyArray()
        set(value) = sharedPrefs.edit { putStringSet(KEY_MY_ARRAY, value.toSet()) }

Here, sharedPrefs.edit{...} is provided by the android core ktx library and should be implemented by adding dependency implementation "androidx.core:core-ktx:1.0.2" in appliation level build.gradle.

You can get the instance of SharedPreferences by using code:

val prefs = Prefs(context)

Furthermore, you can create the Singleton object of Prefs and use from anywhere within the app.

val prefs: Prefs by lazy {
    Prefs(App.instance)
}

where, App extends Application and should be included in AndroidManifest.xml

App.kt

class App:Application() {
    companion object {
        lateinit var instance: App
    }

    override fun onCreate() {
        super.onCreate()
        instance = this
    }
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest .....

   <application
        android:name=".App"
        ....

Example Usage:

// get stored value
val myString = prefs.myString

// store value
prefs.myString = "My String Value"

// get stored array
val myStringArray = prefs.myStringArray

// store array
prefs.myStringArray = arrayOf("String 1","String 2","String 3")
Sagar Chapagain
  • 1,683
  • 2
  • 16
  • 26
5

This is my way

for write

SharedPreferences settings = context.getSharedPreferences("prefs", 0);
SharedPreferences.Editor editore = settings.edit();
editore.putString("key", "some value");
editore.apply();

to read

SharedPreferences settings = getSharedPreferences("prefs", 0);
Strings value = settings.getString("key", "");
xerex09
  • 166
  • 14
Lukap
  • 31,523
  • 64
  • 157
  • 244
  • I think you have these flip flopped. The first section is to write and the second is to read. Also your answer doesn't answer my question. I already know how to use SharedPreferences. My question was about best practices when using SharedPreferences. – Kam Sheffield Jan 13 '12 at 18:33
  • I correct it :) . . . sorry if I miss the point but this is how I use them – Lukap Jan 13 '12 at 18:46
0

Let's assume in a project, with multiple developers working on it, they are defining SharedPreference within an Activity like this:

SharedPreferences sharedPref = context.getSharedPreferences("prefName", 0);

At one point or another two developers can define the SharedPreference with the same name or insert equivalent Key - Value pairs, which will lead to problems in using the keys.

The solution relies on two options, whether to use;

  1. SharedPreferences Singleton that uses String keys.

  2. SharedPreferences Singleton that uses Enum keys.

Personally and According to this Sharepreference Documentation, I prefer to use Enum keys as it enforces stricter control when there are multiple programmers working on a project. A programmer has no choice but to declare a new key in the appropriate enum class and so all the keys are in the same place.

And to avoid boilerplate code writing create the SharedPreference singleton. This SharedPreferences singleton Class help to centralize and simplify reading and writing of SharedPreferences in your Android app.

The source code for the two provided solutions can be found in GitHub

Zoe
  • 27,060
  • 21
  • 118
  • 148
eli
  • 8,571
  • 4
  • 30
  • 40