1

I've got a Preferenceactivity with a EditTextPreference.

What I'm looking for is the command to access the inserted text of the EditTextPreference from a fragment.

What I have so far:

SharedPreferences preferences = this.getActivity().getSharedPreferences("pref", Context.MODE_PRIVATE);
        String name = preferences.getString("edit_text_preference_name", "Default");

I allways get "Default" instead of my actual inserted text from the EditTextPreference.

Thanks in Advance.

Edit:

from SettingsActivity.java

 @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public static class BarcodePreferenceFragment extends PreferenceFragment {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            addPreferencesFromResource(R.xml.pref_barcode);
            setHasOptionsMenu(true);

            bindPreferenceSummaryToValue(findPreference("edit_text_preference_barcode"));
            bindPreferenceSummaryToValue(findPreference("edit_text_preference_name"));
        }
}

pref.xml

<EditTextPreference
        android:capitalize="words"
        android:defaultValue="@string/pref_default_display_name"
        android:key="edit_text_preference_name"
        android:maxLines="1"
        android:selectAllOnFocus="true"
        android:singleLine="true"
        android:title="@string/pref_default_display_name" />
Makarele
  • 59
  • 9
  • Show us the source of `PreferenceActivity`, because it seems to me that you try to access the field from the shared pref called "pref" while your `PreferenceActivity` saves it to a different one (probably the [default](https://developer.android.com/reference/android/preference/PreferenceManager.html#getDefaultSharedPreferences(android.content.Context))). – Gergely Kőrössy Jun 23 '17 at 18:14
  • .java file, .xml or both? – Makarele Jun 23 '17 at 18:19
  • The Java one. I assume the key of the `EditTextPreference` is `edit_text_preference_name`. – Gergely Kőrössy Jun 23 '17 at 18:23

1 Answers1

1

From the documentation of 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.

This means that the PreferenceFragment saves the values to the default shared preferences which leaves you two options:

Option 1 - Use the default SharedPreferences to retrieve the saved value

It's pretty simple, you need to call the PreferenceManager's getDefaultSharedPreferences(...) static method to access the default shared prefs. So instead of

SharedPreferences preferences = this.getActivity().getSharedPreferences("pref", Context.MODE_PRIVATE);
String name = preferences.getString("edit_text_preference_name", "Default");

do

// use getActivity() instead of getContext() if you're using the framework Fragment API and min SDK is lower than 23
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getContext());
String name = preferences.getString("edit_text_preference_name", "Default");

Option 2 - Set your PreferenceFragment to use named shared prefs

You can set the name of the used shared prefs in your BarcodePreferenceFragment's onCreate(...) method by calling setSharedPreferencesName(...) on the belonging PreferenceManager:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getPreferenceManager().setSharedPreferencesName("pref");
    // the rest of your code
}
Gergely Kőrössy
  • 5,620
  • 3
  • 28
  • 44
  • Firt of all, thank you very much! Option 1 works perfectly for me. I only have one problem, my app must support the minimum API of 16 and "getContext" was introduced with API 23. Can you help me with that? I'll accept your answer anyway, but it would be really great if you could help me with this! – Makarele Jun 23 '17 at 19:44
  • Sorry, I usually use support fragments which has `getContext()`. You can use `getActivity()` instead if the fragment has been attached to the `Activity` already. – Gergely Kőrössy Jun 23 '17 at 20:06