Reputation: 445
I am using my own SharedPreferences in a normal Activity with a custom layout with two checkboxes and seekbars. Now I want to make a proper SettingsActivity/SettingsFragment with these settings. Is there a way to use my custom SharedPreference instead of the PreferenceManager and the default preference file?
My own preferecne:
sharedPreferences = getSharedPreferences(getResources().getString(R.string.settingTimetable), MODE_PRIVATE);
Upvotes: 0
Views: 1936
Reputation: 409
Quoted from this answer: https://stackoverflow.com/a/17995236/3691378
You have to manipulate the PreferenceManager of the SettingsFragment. This is what it looks like
// Constants
//--------------------------------------------------------------------------
private final static String TAG = SettingsFragment.class.getName();
public final static String SETTINGS_SHARED_PREFERENCES_FILE_NAME = TAG + ".SETTINGS_SHARED_PREFERENCES_FILE_NAME";
// Life-cycle
//--------------------------------------------------------------------------
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "onCreate()");
// Define the settings file to use by this settings fragment
getPreferenceManager().setSharedPreferencesName(SETTINGS_SHARED_PREFERENCES_FILE_NAME);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.preferences);
}
Then you can access this settings file outside of the fragment like this:
SharedPreferences preferences = getActivity().getSharedPreferences(
SettingsFragment.SETTINGS_SHARED_PREFERENCES_FILE_NAME,
Context.MODE_PRIVATE);
Upvotes: 4