I am new to Android development. In my application, when the user changes the preference, it is necessary to call a function to update some variables.
This is my current code:
mypref.setOnPreferenceChangeListener(
new OnPreferenceChangeListener() {
public boolean onPreferenceChange(Preference arg0, Object arg1) {
if(arg1.toString().matches("...") == false) {
...
return false;
}
...
updateVariables();
return true;
}
});
The problem is that when called updateVariables(), the preference value is not yet updated, and the function sees the old value.
private void updateVariables()
{
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
Map<String, ?> savedKeys = sharedPref.getAll();
for (Map.Entry<String, ?> entry : savedKeys.entrySet()) {
}
}
What would be the least invasive solution? Thank!
source
share