Android shared preferences not saving

前端 未结 5 498
庸人自扰
庸人自扰 2020-12-05 13:12

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 tha

5条回答
  •  隐瞒了意图╮
    2020-12-05 13:35

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

提交回复
热议问题