What's the difference between getDefaultSharedPreferences() and getPreferences()?
Asked Answered
M

4

17

I'm currently taking the "Developing Android Apps" Udacity course. In the "Lesson 3: New Activities and Intents > Use SharedPreferences" segment, the instructor asked me to dig around the Android Developer site for how to get the user preferences from SharedPreferences. However, I found it different between the official documentation and the course's solution.

The Udacity course's solution says, to grab a SharedPreferences instance for the PreferenceActivity, you should call:

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());

(where getActivity() is simply the Context here because it's called inside a Fragment.)

While the official documentation on Android Developer Site indicates that you should call:

SharedPreferences prefs = getActivity().getPreferences(Context.MODE_PRIVATE)

So what's the difference between PreferenceManager.getDefaultSharedPreferences(Context context) and Activity.getPreferences(int mode)?

Please note: This question does not involve anything about getSharedPreferences() which requires a file name. It's about the difference between getPreferences() and getDefaultSharedPreferences().

Thanks in advance.

Maser answered 31/10, 2015 at 1:54 Comment(0)
G
25

Acoording to the link you provided to Android documentation

getSharedPreferences() - Use this if you need multiple preferences files identified by name, which you specify with the first parameter.

getPreferences() - Use this if you need only one preferences file for your Activity. Because this will be the only preferences file for your Activity, you don't supply a name.

So it is, use getSharedPreferences when the data you want to save/retrieve can be used from different activities in the app. If those preferences will only be used in one Activity, you can use getPreferences.

Edit: also note that as said in the post you linked 'getDefaultSharedPreferences will use a default name like "com.example.something_preferences", but getSharedPreferences will require a name'

Gabriello answered 31/10, 2015 at 2:10 Comment(1)
the question did not address getsharedprefereces... it was about the differences between getdefaultsharedpreference and getpreferences... also note that if a preference key/value is stored using getshared preferences or getpreferences it will not be available as a default preference in another activity... you know like the question asked about the differences between the things you didn't address...Coaly
C
16

One of the major differences: getPreferences () returns a file only related to the activity it is opened from. While getDefaultSharedPreferences () returns the application's global preferences. Learned that the hard way yesterday.

Coaly answered 5/7, 2016 at 22:12 Comment(2)
Here you can find the relevant documentation: developer.android.com/guide/topics/ui/…Jaella
If I understand you correctly, calling getPreferences() in two different activities will result in two different files, right?Jorgenson
A
10

If you take a look inside PreferenceManager:

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

/**
 * Returns the name used for storing default shared preferences.
 *
 * @see #getDefaultSharedPreferences(Context)
 * @see Context#getSharedPreferencesPath(String)
 */
public static String getDefaultSharedPreferencesName(Context context) {
    return context.getPackageName() + "_preferences";
}

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

So getDefaultSharedPreferences() use getSharedPreferences() method with your app package name and mode private, if you use getPreferences() from activity it will use the same getSharedPreferences() method but with getLocalClassName();

Alage answered 2/11, 2016 at 7:46 Comment(0)
L
1

From the first article linked below: "Note: The SharedPreferences APIs are only for reading and writing key-value pairs and you should not confuse them with the Preference APIs, which help you build a user interface for your app settings (although they use SharedPreferences as their implementation to save the app settings)."

http://developer.android.com/training/basics/data-storage/shared-preferences.html

http://developer.android.com/guide/topics/ui/settings.html

Lys answered 31/10, 2015 at 2:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.