Android shared preferences not saving

2019-01-17 14:23发布

I've created an Android live wallpaper and i'm trying to let a user choose an image from their phone and apply it as a background image, but when I launch the activity that start the intent to pick the images, my shared preferences don't seem to save properly.

Below is my onCreate method of the activity I start when the users presses the preference button, and the onActivityResult which gets the path of the image on the device (all that seems to work). The println after I commit the preferences prints out nothing.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
    photoPickerIntent.setType("image/*");
    startActivityForResult(photoPickerIntent, SELECT_PICTURE);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (resultCode == RESULT_OK) {
        if (requestCode == SELECT_PICTURE) {
            Uri selectedImageUri = data.getData();
            selectedImagePath = getPath(selectedImageUri);

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

            System.out.println("Image" + preferences.getString(SETTINGS_BACKGROUND_IMAGE, ""));
        }
    }

    finish();
}

4条回答
Summer. ? 凉城
2楼-- · 2019-01-17 14:45

In my case I had to add editor.apply(); before commit in order to work.

This is my code:

preferences = getApplicationContext().getSharedPreferences(PREFERENCES_NAME, 0);
SharedPreferences.Editor editor = preferences.edit();
editor.putString(SETTINGS_BACKGROUND_IMAGE, "okok");
editor.apply();//I added this line and started to work...
editor.commit();
查看更多
我命由我不由天
3楼-- · 2019-01-17 14:47

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.commit();
查看更多
倾城 Initia
4楼-- · 2019-01-17 14:50

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();

查看更多
何必那么认真
5楼-- · 2019-01-17 15:07

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();

;)

查看更多
登录 后发表回答