SharedPreferences and PreferenceFragment

I made examples for PreferenceFragments and SharedPreferences on the ANdroid developer website, but now I have a problem. I want to have access to the settings on the Settings screen from different activities, but I don’t know what the name of the preferences file created by my preferences fragment is. Does anyone know anything about specifying a preference file name for a PreferenceFragment?

+4
source share
1 answer

Settings must be saved in the default file.

To access, use PreferenceManager#getDefaultSharedPreferences()

 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences (context); 

context - any valid Context, including actions.

From the documentation for PreferenceFragment :

To get an instance of SharedPreferences, the hierarchy preference in this fragment will be used, call getDefaultSharedPreferences (android.content.Context) with the context in the same package as this fragment.

+14
source

All Articles