Difference between getDefaultSharedPreferences and getSharedPreferences

James picture James · May 10, 2011 · Viewed 86.8k times · Source

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

Answer

copolii picture copolii · Jun 10, 2011

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;
}