Raegtime Raegtime - 10 months ago 69
Android Question

Android SharedPreferences empty after commit

Why is in the following example the foo false?

mPrefs = PreferenceManager.getDefaultSharedPreferences(this);
mPrefs.edit().putBoolean("myValue",true);
mPrefs.edit().commit();

boolean foo = mPrefs.getBoolean("myValue",false);

Answer Source

I think you have to write in this way

mPrefs = PreferenceManager.getDefaultSharedPreferences(this);
mPrefs.edit().putBoolean("myValue",true).commit();

boolean foo = mPrefs.getBoolean("myValue",false);