Shared Preferences get lost after shutting down device or killing the app

前端 未结 3 1008
礼貌的吻别
礼貌的吻别 2020-12-06 05:57

there are lots of questions out there related to shared preferences and the alternatives. My problem: when I shut down the device or kill the app, the shared preferences get

3条回答
  •  星月不相逢
    2020-12-06 06:55

    I've figured out a solution that works both, on my Acer and on my XOOM device: you have to call clear() on the editor before committing new data:

    public void saveCollection(Context context){
        SharedPreferences settings = context.getSharedPreferences(context.getString(R.string.restore_values), 0);
        SharedPreferences.Editor e = settings.edit();
        e.clear();
        e.putStringSet(context.getString(R.string.collection), collection);
        e.commit();
    }
    

提交回复
热议问题