简体   繁体   中英

The use of Listening for preference changes

After reading the Android documentation about settings , i have a few questions about Listening for preference changes

"There are several reasons you might want to be notified as soon as the use changes one of the preferences. In order to receive a callback when a change happens to any one of the preferences, implement the SharedPreference.OnSharedPreferenceChangeListener interface and register the listener for the SharedPreferences object by calling registerOnSharedPreferenceChangeListener()."

I don't really get it , and I couldn't find why it is important to implement this listener, what is the use of it? what does it do? when to use it? right now , without implementing the listener my settings works fine, is it crucial for any settings or just on a particular cases? thanks.

resource : http://developer.android.com/guide/topics/ui/settings.html#Fragment

You aren't required to implement an OnSharedPreferenceChangeListener . It's a capability that's there for convenience.

Sometimes you want to react immediately to a change in preferences. For instance, if you have a "Settings" action where the user can, say, change the background color of an activity, then when the user makes the selection, you'd like the background color to change immediately, not when the user restarts the activity. One way to do this is for the activity to check the status of the background preference in onResume() , but another way is for the activity to register an OnSharedPreferenceChangeListener in onStart and (unregister it in onStop ). I've found that using a listener in this way can sometimes lead to simpler code. It also helps greatly when the code that should react to a settings change does not normally participate in the framework's lifecycle methods.

Implementing an OnSharedPreferenceChangeListener is pretty straightforward. You just need either declare your class to implements OnSharedPreferenceChangeListener or implement an object that does. For instance:

public class MyActivity extends Activity {
    . . .
    private final OnSharedPreferenceChangeListener mPrefsListener =
        new OnSharedPreferenceChangeListener() {
            @Override
            public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
                                                  String key)
            {
                if (key.equals(IMPORTANT_PREF_KEY)) {
                    setImportantValue(sharedPrefs.getInt(IMPORTANT_PREF_KEY, -1));
                }
            }
        };

    @Override
    protected void onStart() {
        super.onStart();
        PreferenceManager.getDefaultSharedPreferences(this)
            .registerOnSharedPreferenceChangeListener(mPrefsListener);
    }

    @Override
    protected void onStop() {
        super.onStop();
        PreferenceManager.getDefaultSharedPreferences(this)
            .unregisterOnSharedPreferenceChangeListener(mPrefsListener);
    }

    void setImportantValue(int value) {
         . . .
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM