Real-time settings update

I have a settings screen that has some prefs that are related. This means that if I have pref x and y , I sometimes need y to change something when x changes.

What I'm doing at the moment is listening to the prefix change event, and do the following:

 SharedPreferences.Editor editor = prefs.edit(); editor.putString("y_pref", "somevalue"); editor.commit(); 

The problem is that in order to actually view the change, I must first close the prefs screen and then open it again, only this way I will see the newly installed prefs.

Is there a way to change the prefix so that the changes are visible immediately, without having to reload the prefs screen?

+1
source share
1 answer

Try calling the installer of your preference, updating it yourself instead:

eg. EditTextPreference . setText () . Thus, the setting itself also updates its own value. If you do the update yourself, the preference will not receive the new value, because it does not even know that the stored value has changed.

If you have a PreferenceFragment , you may prefer PreferenceFragment.findPreference () .

If you have PreferenceActivity , you can get PreferenceActivity.findPreference () .

You call this using the preference key that you assigned in your XML settings file, and you get an instance of the corresponding preference. Then you added it to CheckBoxPreference , EditTextPreference , etc. (The type you specified in your XML file).

+4
source

All Articles