android – when use getDefaultSharedPreferences and getSharedPreferences-ThrowExceptions

Exception or error:

I just curious. There are 3 method:

1. getPreferenceManager().setSharedPreferencesName(String PrefName);

2. PreferenceManager.getDefaultSharedPreferences(Context context)

3. Context.getSharedPreferences (String name, int mode)

As I know, the third method is only used when the first method is used, right?
But with 3 method we also use addPreferencesFromResource(int resID);
so, what is the difference? When can we use one of these method?

How to solve:

Let’s go one step at a time:

  1. setSharedPreferencesName() is method that allows to set the name of the preference group for later use. This is helpful for example when using the helper class of
    PreferencesActivity before loading a preferences from XML resource file by calling addPreferencesFromResource(). It is therefore not as common as the other 2 methods you mentioned above.

  2. getDefaultSharedPreferences() uses a default name, usually stored as /data/data/
    It is commonly used. Note that this default is set per application.

    The alternative method – getSharedPreferences() requires to indicate a specific preference (file) name and an operation mode.

  3. As appears also in another answer about shared preferences,
    getDefaultSharedPreferences() in fact uses Context.getSharedPreferences, so the result is the same, but without the flexbility to split to multiple preference files, that is offered by 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.

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


getDefaultSharedPreferences() uses a default preference-file name like "com.example.something_preferences". 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/

getSharedPreference is the best way because using getDefaultSharedPreferences has some flaws

  1. Actualy getDefaultSharedPreferences doesn’t work correct on some
    devices when build with targer api 13
  2. Starting app from shortcut and from menu gives me different
    DefaultSharedPreferences. After removing DefaultSharedPreferences
    from my code – it works perfect. I can’t just say: people dont make
    shrotcuts, so I had to change code

This link may also help

Leave a Reply

Your email address will not be published. Required fields are marked *