Skip to content Skip to sidebar Skip to footer

How To Create A Listener To Preferences Changes In Preferences Activity?

I have Preferences activity in my app that has ListPreference so the user can choose language for the app. The app displays the new language just after the user close the Preferenc

Solution 1:

Here is some real quick sample code for a shared prefs chaneg listener I have set-up in one of my projects; it's located in the onCreate of a Service but obviously can detect changes to my shared prefs that originate from anywhere in my app.

private SharedPreferences.OnSharedPreferenceChangeListener listener;


//Loads Shared preferences
prefs = PreferenceManager.getDefaultSharedPreferences(this);

//Setup a shared preference listener for hpwAddress and restart transport
listener = new SharedPreferences.OnSharedPreferenceChangeListener() {
            public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
           if (key.equals(/*key for shared pref you're listening for*/) {
               //Do stuff; restart activity in your case
            }
        };

prefs.registerOnSharedPreferenceChangeListener(listener);

Post a Comment for "How To Create A Listener To Preferences Changes In Preferences Activity?"