PreferenceFragment
is easy to create a configuration screen, but it assumes that you use the default SharedPreferences
and can only edit global settings in the app.
Would it be possible to link any SharedPreferences
by name to the PreferenceFragment
screen?The original purpose is to easily implement the App Widgets instance-specific configuration screen.
Before calling addPreferencesFromResource(R.xml.preferences)
in the PreferenceFragment onCreate, you can specify the name of the SharedPreferences to use by using getPreferenceManager().setSharedPreferencesName
.
getPreferenceManager().setSharedPreferencesName(name)?See you.
Do you mean PreferenceManager#getDefaultSharedPreferences when you say getDefaultSharedPreferences on which PreferenceFragment depends?
Once you have set the name in PreferenceManager#setSharedPreferencesName, do not use PreferenceManager#getDefaultSharedPreferences, but use the name you set in PreferenceManager#getSharedPreferences or Context#getSharedPreferences
The SharedPreferences name returned by PreferenceManager#getDefaultSharedPreferences is hardcoded with "package name +_preferences" as far as the source code goes, so it is unlikely to be changed.
© 2024 OneMinuteCode. All rights reserved.