How to Listen for Preference Changes Within a Preferencefragment

How to listen for preference changes within a PreferenceFragment?

I believe you just need to register/unregister the Listener in your PreferenceFragment and it will work.

@Override
public void onResume() {
super.onResume();
getPreferenceManager().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);

}

@Override
public void onPause() {
getPreferenceManager().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
super.onPause();
}

Depending on what you want to do you may not need to use a listener. Changes to the preferences are committed to SharedPreferences automatically.

How to create a Listener to Preferences changes in Preferences activity?

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);

The use of Listening for preference changes

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) {
. . .
}
}

Android changing Dialog content of a PreferenceFragment

You should implement Preference.OnPreferenceChangeListener for PreferenceFragment class. Then override onPreferenceChange method and if newValue is correct return true otherwise return false. If you return false then preference value will not change.

public class SettingsFragment extends PreferenceFragmentCompat implements
Preference.OnPreferenceChangeListener {

@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {

int number = 0;
try{
number = Integer.parseInt(newValue.toString());
}catch(Exception e){

}

if (number > 0 && number < 10){ //It's an example
return true; // Preference will change
}

return false; // newValue rejected and preference not change
}

}

After that you must set this listener to the corresponding preference. Do it inside onCreatePreference.

@Override
public void onCreatePreferences(Bundle bundle, String s) {

addPreferencesFromResource(R.xml.settings);

PreferenceScreen prefScreen = getPreferenceScreen();
Preference editTextPrefernce = prefScreen.findPreference(getString(R.string.pref_key));

if (editTextPrefernce != null && editTextPrefernce instanceof EditTextPreference) {
editTextPrefernce.setOnPreferenceChangeListener(this);
}
}

Enjoy it.



Related Topics



Leave a reply



Submit