[关闭]
@natsumi 2016-07-18T15:22:13.000000Z 字数 860 阅读 1237

监听用户对PreferencesFragment设置的修改

Android


两个回调函数要分清楚!
onPreferenceChange:用户修改了preference的值时调用
onSharedPreferenceChanged:设置sharedpreference改变、被添加或被移除时调用

  1. /**
  2. * Called when a Preference has been changed by the user. This is
  3. * called before the state of the Preference is about to be updated and
  4. * before the state is persisted.
  5. *
  6. * @param preference The changed Preference.
  7. * @param newValue The new value of the Preference.
  8. * @return True to update the state of the Preference with the new value.
  9. */
  10. boolean onPreferenceChange(Preference preference, Object newValue);
  1. /**
  2. * Called when a shared preference is changed, added, or removed. This
  3. * may be called even if a preference is set to its existing value.
  4. *
  5. * <p>This callback will be run on your main thread.
  6. *
  7. * @param sharedPreferences The {@link SharedPreferences} that received
  8. * the change.
  9. * @param key The key of the preference that was changed, added, or
  10. * removed.
  11. */
  12. void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key);
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注