SharedPreferences sharedPreferences = ...;
sharedPreferences.registerOnSharedPreferenceChangeListener(mOnSharedPreferenceChangeListener);
private final SharedPreferences.OnSharedPreferenceChangeListener mOnSharedPreferenceChangeListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
//TODO
}
}
Please note:
registerOnSharedPreferenceChangeListener
stores it with a weak reference, so it would be garbage collected;registerOnSharedPreferenceChangeListener(this);
unregisterOnSharedPreferenceChangeListener
.