Search code examples
androidpreference

How to update ListPreferences after implementing OnChangeListener


I have PrefActivity and I use OnChange Listener to make a toast when ever user change any button in list preferences. But now I have 2 problems: 1-first time that user change an option toast is not shown 2-after that, when ever user change prefrences, the value of list is not updated, and is always set on second value.

this is my code:

public class PrefsActivity extends PreferenceActivity implements OnSharedPreferenceChangeListener{

private ListPreference myPreference;
@Override
 public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); 
addPreferencesFromResource(R.xml.prefs);

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
prefs.registerOnSharedPreferenceChangeListener(this);

}

 public void onSharedPreferenceChanged(SharedPreferences arg0, String key) {

ListPreference lp = (ListPreference) findPreference("blocktype");
 lp.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {

    @Override
    public boolean onPreferenceChange(Preference preference, Object newValue) {
        // TODO Auto-generated method stub

        Toast.makeText(PrefsActivity.this, "second", Toast.LENGTH_LONG).show();
        return false;


    }

});
}
}

What is


Solution

  • As no one answered my question I figured out, where is problem.

    return false
    

    should be changed to

    return true
    

    in order to update the preferences