Android shared preferences not saving

Try another way of initializing your SharedPreferences variable:

SharedPreferences sf = PreferenceManager.getDefaultSharedPreferences(this);

You can also chain writing to sf with sf.edit().putString(string, value).commit();


From the documentation:

Create a new Editor for these preferences, through which you can make modifications to the data in the preferences and atomically commit those changes back to the SharedPreferences object.

Since that's a new Editor instance, your code should be more like this:

preferences = getApplicationContext().getSharedPreferences(PREFERENCES_NAME, 0);
SharedPreferences.Editor editor = preferences.edit();
editor.putString(SETTINGS_BACKGROUND_IMAGE, "okok");
editor.apply();

well, based on @zrgiu post, for me only worked adding editor.clear(); before use the Editor...so the final code will be something like:

preferences = getApplicationContext().getSharedPreferences(PREFERENCES_NAME, 0);
SharedPreferences.Editor editor = preferences.edit();
editor.clear();
editor.putString(SETTINGS_BACKGROUND_IMAGE, "okok");
editor.commit();

;)