androidandroid-sharedpreferences

Are SharedPreference instances cached? (Through PreferenceManager.getDefaultSharedPreferences())


In my app I call:

PreferenceManager.getDefaultSharedPreferences(context);    

Which in turn calls this internally.

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

Context is abstract so implementations may derive from each other, but what Im wondering is whether or not a new instance of SharedPreferences is created with each call; or if it re-uses an instance internally?


Solution

  • There is only one instance of SharedPreferences when you call that. Refer to context.getSharedPreferences docs

    For any particular set of preferences, there is a single instance of this class that all clients share.