I am developing the preferences for an Android application, for this I have created the file "preferences.xml" where I set the preferences and its summary from the default value.
<EditTextPreference
android:key="pref_ciudad"
android:title="Ciudad"
android:summary="Cáceres"
android:selectAllOnFocus="true"
android:singleLine="true"
android:defaultValue="Caceres" />
In the fragment of preferences, I have declared a listener that detects that the preference has been changed, updating the summary field and making it persistent.
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key ) {
if (key.equals(KEY_PREF_CIUDAD)){
Preference ciudadPref = findPreference( key );
ciudadPref.setSummary(sharedPreferences.getString(key, ""));
ciudadPref.setPersistent( true );
}
}
The problem arises when returning to the fragment of preferences, which re-executes the onCreate method and fixes the summary with the default value, therefore, it does not correspond to the content of the preference at that moment.
Thank you very much!