Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onPreferenceChange method isn't called when Listpreferences changes

I have a Preferences Activity in my code. I want to show a toast when ever user change radio button in a ListPreferences. I used this code but it does not work. :(

public class PrefsActivity extends PreferenceActivity implements OnSharedPreferenceChangeListener {

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.prefs);   
    }

    public void onSharedPreferenceChanged(SharedPreferences arg0, String key) {

        ListPreference listPreference = (ListPreference) findPreference(key);
        listPreference.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 my problem?

like image 913
Reza_Rg Avatar asked Dec 02 '12 06:12

Reza_Rg


1 Answers

Change your onCreate code to:

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

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

You are not adding the registerOnSharedPreferenceChangeListener to PrefsActivity Activity.

like image 200
ρяσѕρєя K Avatar answered Feb 27 '23 04:02

ρяσѕρєя K