I'm developing an application in which I'm storing username
and password
in SharedPreferences
. All things are working fine for me, storing as well as retrieving the values. But I discovered that when I restart the device or the app is force closed the value stored in SharedPreferences
is reset. And when I again launch my app I get null values in SharedPreferences
key. Here, is what I'm doing for storing the values:
SharedPreferences emailLoginSP;
emailLoginSP = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
emailLoginSP.edit().putString("prefEmailId", email_text).commit();
emailLoginSP.edit().putString("prefUserId", userIDToken).commit();
emailLoginSP.edit().putString("prefAccess_token", accessToken).commit();
Intent i = new Intent(LoginWithEmail.this,UserInfoActivity.class);
i.putExtra("acess_token", accessToken);
i.putExtra("user_id", userIDToken);
i.putExtra("emailID", email_text);
startActivity(i);
And, this is how I'm retriving it:
SharedPreferences emailLoginSP = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
loginEmail = emailLoginSP.getString("prefEmailId", null);
loginUserId = emailLoginSP.getString("prefUserId", null);
loginAccessToken = emailLoginSP.getString("prefAccess_token", null);
All things are working fine till now. Again I'm stating my problem that I get null values when I force close or restart my device. Can we store it permanently in the app memory? Or, I'm doing something wrong here?
Any help will be appreciated.