Difference between getDefaultSharedPreferences and getSharedPreferences

AndroidAndroid PreferencesSharedpreferences

Android Problem Overview


What is the difference between getDefaultSharedPreferences and getSharedPreferences in Android? Can anyone please explain?

Android Solutions


Solution 1 - Android

getDefaultSharedPreferences will use a default name like "com.example.something_preferences", but getSharedPreferences will require a name.

getDefaultSharedPreferences in fact uses Context.getSharedPreferences (below is directly from the Android source):

public static SharedPreferences getDefaultSharedPreferences(Context context) {
    return context.getSharedPreferences(getDefaultSharedPreferencesName(context),
        getDefaultSharedPreferencesMode());
}

private static String getDefaultSharedPreferencesName(Context context) {
    return context.getPackageName() + "_preferences";
}

private static int getDefaultSharedPreferencesMode() {
    return Context.MODE_PRIVATE;
}

Solution 2 - Android

Let's review the basic points of difference:

  1. getDefaultSharedPreferences() uses a default preference-file name. This default is set per application, so all activities in the same app context can access it easily as in the following example:

     SharedPreferences spref = PreferenceManager.getDefaultSharedPreferences(this);
     if (spref.contains("email")) {
     	 String sEmailAddr = spref.getString("email", "");
     }
    

The preferences are usually stored at /data/data/com.package.name/shared_prefs/com.package.name_preferences.xml.

  1. The alternative method - getSharedPreferences(name,mode) requires to indicate a specific preference (file) name and an operation mode (e.g. private, world_readable, etc.)

As mentioned by copolii, the result is the same, but the first option is simpler and lacks the flexibility to split to multiple preference files, that is offered by the second option of getSharedPreferences(). Sharing the preferences between apps using a MODE_WORLD_READABLE operation indicator is also something possible using getSharedPreferences(), but is rarely used.

IMHO, getDefaultSharedPreferences() can be safely used without going into the confusion of multiple preference file names that are prone to typos and confusion, unless you want that different modules in your app will use different preference files. Normally this is not needed. If an app needs to save a lot of parameters, probably using external database will be better as it offers also better data protection.

If someone knows of a good reason to regularly use getSharedPreferences() and not getDefaultSharedPreferences(), please let me know by commenting here.

Solution 3 - Android

I know this post is a bit old, but since 24.0.1 of the v7 support library you can retrieve the default preferences by context anywhere using

// context might be an application context, activity, ..
// so if you want to get your apps defaults, pass an activity context
PreferenceManager.getDefaultSharedPreferences(context)

See https://developer.android.com/reference/android/support/v7/preference/PreferenceManager#getdefaultsharedpreferences

Solution 4 - Android

There's a 3rd function as well:

public SharedPreferences Activity.getPreferences(int mode) {}

See my question and answer here: https://stackoverflow.com/questions/37953002/mess-with-the-shared-preferences-of-android-which-function-to-use/37953072

Solution 5 - Android

Both getSharedPreferences() and getDefaultSharedPreferences() are used to access application-level preferences .getDefaultSharedPreferences() is used to get the shared preferences that work in accordance with Android’s overall preference framework. getDefaultSharedPreferences() is better to use as it gives the SharedPreferences object that works with a PreferenceActivity by default.

Solution 6 - Android

Be aware that using default shared preferences is NOT the same as using shared preferences with your package name:

context.getSharedPreferences(getPackageName(), MODE_PRIVATE);

=> Name of shared preferences: "com.my.packagename"

PreferenceManager.getDefaultSharedPreferences(context);

=> Name of shared preferences: "com.my.packagename_preferences"

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJamesView Question on Stackoverflow
Solution 1 - AndroidcopoliiView Answer on Stackoverflow
Solution 2 - AndroidwiztrailView Answer on Stackoverflow
Solution 3 - AndroidMakiboView Answer on Stackoverflow
Solution 4 - AndroidHack06View Answer on Stackoverflow
Solution 5 - AndroidAndroid DeveloperView Answer on Stackoverflow
Solution 6 - AndroidRichard RView Answer on Stackoverflow