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

后端 未结 6 1891
暖寄归人
暖寄归人 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:01

    Change to:

    SharedPreferences emailLoginSP;
    SharedPreferences.Editor SPEdit;
    
    emailLoginSP = getSharedPreferences("pref_file_name",MODE_PRIVATE);
    SPEdit = emailLoginSP.edit();
    
    SPEdit.putString("prefEmailId", email_text);
    SPEdit.putString("prefUserId", userIDToken);
    SPEdit.putString("prefAccess_token", accessToken);
    SPEdit.commit();
    

    NOTE: This is untested and from memory, so there may be an error.

    You want to minimise the .commit() calls, which is why there is only one at the end.

    pref_file_name can be whatever you want, with lower case letters, no numbers at start, etc.

提交回复
热议问题