I've an Android application with a setting menu. I've some EditTextPreferences that when is changed a button in the UI changes too. Moreover, I would like to implemente a Preference that reset the values of all EditTextPreferences. Now I have:
preference.xml
<PreferenceScreen
android:key="custom_balizamiento"
android:persistent="false"
android:title="@string/balizamiento" >
<EditTextPreference
android:defaultValue="@string/custom_event_1"
android:key="custom_event_balizamiento_1"
android:title="@string/custom_event_1" />
<EditTextPreference
android:defaultValue="@string/custom_event_2"
android:key="custom_event_balizamiento_2"
android:title="@string/custom_event_2" />
</PreferenceScreen>
<Preference
android:key="button_reset"
android:summary="@string/pref_reset_summary"
android:title="@string/pref_reset" />
SettingsFragment.java
Preference buttonreset = (Preference) findPreference("button_reset");
buttonreset.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference arg0) {
pref.edit().clear();
pref.edit().commit();
pref.edit().apply();
updatePreference();
pref.edit().commit();
pref.edit().apply();
return true;
}
});
public void updatePreference() {
Map<String, ?> keys = pref.getAll();
for (Map.Entry<String, ?> entry : keys.entrySet()) {
if (entry.getKey().contains("custom_event")) {
Preference auxpref = findPreference(entry.getKey());
String newValue = pref
.getString(entry.getKey(), entry.getKey());
auxpref.setTitle(newValue);
}
}
pref.edit().commit();
}
When I pulse the reset prefence, nothing change. However, when I close the settings fragment and open it again the preference and the button in the UI changes to the default value. How can I update the button and the preference when I pulse the reset preference?