Shared Preferences reset data when app is force closed or device is restarted

后端 未结 6 1896
暖寄归人
暖寄归人 2020-11-30 11:21

I\'m developing an application in which I\'m storing username and password in SharedPreferences. All things are working fine for me, s

6条回答
  •  不知归路
    2020-11-30 12:08

    Make sure that you are using the same file to save and retrieve. This made me look really dumb :(

    // When saving
    public SharedPreferences.Editor getEditor(Activity activity, int mode) {
        SharedPreferences sharedPref = activity.getApplicationContext().getSharedPreferences(PreferenceConstants.SWAGGER_USER_PREFS, mode);
        SharedPreferences.Editor editor = sharedPref.edit();
        return editor;
    }
    
    
    // When getting it
        public SharedPreferences getSharedPref(Activity activity, int mode) {
                SharedPreferences sharedPref = activity.getSharedPreferences(PreferenceConstants.SWAGGER_USER_PREFS, mode);
                return sharedPref;
            }
    

提交回复
热议问题