Skip to content


SharedPreferences editor’s commit doesnt work?

Upon reading the documentation on SharedPreferences edit() carefully, it actually says that the method returns a new editor instance. This means that the following doesnt actually do anything:

prefs.editor().putString("name1","val");

prefs.editor().putString("name2","val");

prefs.editor().commit();

Instead, this must be done:

SharedPreferences.Editor ed = prefs.edit();

ed.putString("name1","val");

ed.putString("name2","val");

ed.commit();

Or, for single values we can do:

prefs.editor().putString("name2","val").commit();

Posted in Android, Java. Tagged with , , , .