Search code examples
androidfragmentsettingspreferences

Loading previously stored data from SharedPreferences into PreferenceFragment


I've followed the documentation to create a Preference Fragment. All of my preferences are working as expected, but I have a first and last name setting that I'd like to load dynamically from SharedPreferences. These values have already been stored when the user logged in.

Their values in strings.xml:

 <string name="first_name">first_name</string>
 <string name="last_name">last_name</string>

Them being saved on login:

editor.putString(mContext.getResources().getString(R.string.first_name), firstName);
editor.putString(mContext.getResources().getString(R.string.last_name), lastName);
editor.commit();

The preferences in preferences.xml:

   <EditTextPreference
                android:key="@string/first_name"
                android:inputType="text"
                android:persistent="true"
                android:title="First Name"/>
    <EditTextPreference
                android:key="@string/last_name"
                android:inputType="text"
                android:persistent="true"
                android:title="Last Name"/>

Trying to load them in Preference Fragment:

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        sessionManager = SessionManager.getInstance(getActivity());
        addPreferencesFromResource(R.xml.preferences);
        PreferenceManager.setDefaultValues(getActivity(), R.xml.preferences, false);

        // Set first and last name preference default values
        firstNamePreference = (EditTextPreference)findPreference("first_name");
        lastNamePreference = (EditTextPreference)findPreference("last_name");
        nearbyPreference = (CheckBoxPreference)findPreference("show_places");
//        firstNamePreference.setText(sessionManager.getUserFirstName());
//        firstNamePreference.setSummary(sessionManager.getUserFirstName());
//        lastNamePreference.setText(sessionManager.getUserLastName());
//        lastNamePreference.setSummary(sessionManager.getUserLastName());
        nearbyPreference.setDefaultValue(true);
    }

So I can set them manually (as well as the summary), but I'd really like to set them from the already set values in SharedPreferences. How can I do this?


Solution

  • So after looking through my code, I realized that my application stores preferences in a file other than the default. So, I had to do the following from this article:

    @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            addPreferencesFromResource(R.xml.preferences);
            PreferenceManager.setDefaultValues(getActivity(), R.xml.preferences, false);
    
    
            sharedPreferences = getActivity().getSharedPreferences("file_name", 0); // 0 is private
    
            // show the current value in the settings screen
            for (int i = 0; i < getPreferenceScreen().getPreferenceCount(); i++) {
                pickPreferenceObject(getPreferenceScreen().getPreference(i));
            }
        }
    
    private void pickPreferenceObject(Preference p) {
            if (p instanceof PreferenceCategory) {
                PreferenceCategory cat = (PreferenceCategory) p;
                for (int i = 0; i < cat.getPreferenceCount(); i++) {
                    pickPreferenceObject(cat.getPreference(i));
                }
            } else {
                initSummary(p);
            }
        }
    
    private void initSummary(Preference p) {
            if (p instanceof EditTextPreference) {
                EditTextPreference editTextPref = (EditTextPreference) p;
                String value = sharedPreferences.getString(editTextPref.getKey(), "");
                p.setSummary(value);
                ((EditTextPreference) p).setText(value);
            }
            // More logic for ListPreference, etc...
        }
    

    Otherwise you could probably just set them each with the default prefs, accessed like so:

    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity());