I store my boolean like this in my preference activity:
OnPreferenceChangeListener locaListener = new OnPreferenceChangeListener() {
public boolean onPreferenceChange(Preference preference, Object newValue) {
SharedPreferences.Editor editor = getPreferences(MODE_PRIVATE).edit();
if(newValue.toString().trim().equals("true")){
editor.putBoolean("locationEnabled", true);
}else if (newValue.toString().trim().equals("false")){
editor.putBoolean("locationEnabled", false);
}
editor.commit();
return true;
}
};
and try to retrieve it like this in my main activity:
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
boolean prefLocationEnabled = prefs.getBoolean("locationEnabled", false);
Same approach with string works perfect but with boolean it seems to return always the default value which is false. Anyone knows what am I doing wrong?
Once you call getPreferences(MODE_PRIVATE) when you save and when you load you call PreferenceManager.getDefaultSharedPreferences(this);
I think it’s not same preferences. Use only one of these