RScottCarson
RScottCarson

Reputation: 980

SharedPreferences and PreferenceFragment

I have done the examples for PreferenceFragments and SharedPreferences on the ANdroid developer website, however I'm running into an issue now. I want to be able to access the preferences in my "Settings" screen from different Activities but I don't know what the name of the preference file generated by my preference fragment is. Does anyone know anything about specifying the preference filename for a PreferenceFragment?

Upvotes: 5

Views: 8285

Answers (1)

A--C
A--C

Reputation: 36449

The Preferences should be saved to the default file.

To access, use PreferenceManager#getDefaultSharedPreferences()

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences (context);

context is any valid Context, including Activities.

From the documentation on PreferenceFragment:

To retrieve an instance of SharedPreferences that the preference hierarchy in this fragment will use, call getDefaultSharedPreferences(android.content.Context) with a context in the same package as this fragment.

Upvotes: 14

Related Questions