Difference between getDefaultSharedPreferences and getSharedPreferences
Solution 1
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
Let's review the basic points of difference:
-
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
. 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
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)
Solution 4
There's a 3rd function as well:
public SharedPreferences Activity.getPreferences(int mode) {}
See my question and answer here: Mess with the shared preferences of android - which function to use?
Solution 5
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"
Related videos on Youtube
James
Updated on March 06, 2020Comments
-
James over 4 years
What is the difference between
getDefaultSharedPreferences
andgetSharedPreferences
in Android? Can anyone please explain?-
LarsH about 8 yearsAn almost-too-obvious difference is that one is static and one is not. But it doesn't really matter, since you need a
Context
instance in order to call either one.
-
-
Martin about 12 yearsTo bad
getDefaultSharedPreferencesName
is not public as the name is needed for the backup / restore framework. -
Kostadin about 12 yearsMy reason is: stackoverflow.com/questions/10786172/…
-
Dr.jacky over 11 yearsWhat about stackoverflow.com/questions/10786172/… ?
-
Dr.jacky over 11 yearsAnd how to listen to preference change , in getSharedPreferences mode that use custom name for preference ? (not use default name like getSharedPreferences "com.example.something_preferences")
-
Dr.jacky over 11 yearsAnd how to listen to preference change , in getSharedPreferences mode that use custom name for preference ? (not use default name like getSharedPreferences "com.example.something_preferences")
-
Mr_and_Mrs_D about 11 yearsCould you please add a link to the source ?
-
copolii about 11 years@Mr_and_Mrs_D it's easier to look it up. If I add a link it would be to a specific API version, which may or may not be what you're looking for.
-
Zapnologica about 9 yearsso they are exactly the same? the one is just a helper to make calling it easier?
-
tarn almost 8 yearsThis is a wrapper for getSharedPreferences() from the Activity source code: public SharedPreferences getPreferences(int mode) { return getSharedPreferences(getLocalClassName(), mode); }
-
copolii almost 6 years@Zapnologica Correct.
-
Mahmoud over 5 years@Martin the method getDefaultSharedPreferencesName(..) became public starting from API 24.
-
Alex Weavers over 5 yearsDr.jacky I'd say you would wrap up your preferences in some sort of Session-like object: class Session { prefs = getSharedPreferences() ... } and then use getters and setters for the things you want to monitor. When the setters are called, you can trigger events in the setter logic.
-
Kevin about 5 yearsPreferenceManager is being deprecated in Android Q. You can still use the context.getSharedPreferences way of things though. Changed this call to context.getSharedPreferences("${packageName}_preferences", MODE_PRIVATE)
-
FutureShocked about 5 yearsIt looks like PreferenceManager is being deprecated in that it is just being moved to Androix. developer.android.com/reference/androidx/preference/…