Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 704
Anupam Avatar asked Mar 12 '13 05:03

Anupam


3 Answers

Hi ! Solution that worked for me!

Solution 1 Solution 2

Solution 1:

  SharedPreferences sharedPreferences = getSharedPreferences("namefile",               
    Context.MODE_PRIVATE);//store or retrieved file "namefile.xml".
    /*or 
   SharedPreferences sharedPreferences    
   =getActivity().getSharedPreferences("namefile", Context.MODE_PRIVATE); 
   (use Shared Preferences in Fragment)*/
   String getValueFromKey = sharedPreferences.getString("yourKey",new   
   String());
   //Log.d("printf:",getValueFromKey );
   getValueFromKey ="Hahaha"; /*  Edit value    
   or Do nothing …… */
   SharedPreferences.Editor editor = sharedPreferences.edit();
   editor.clear(); //[important] Clearing your editor before using it.
   editor.putString("yourKey", getValueFromKey);
   editor.commit();

Solution 2:

SharedPreferences sharedPreferences = getSharedPreferences("namefile",     
  Context.MODE_PRIVATE);//store or retrieved file "namefile.xml".
/*or 
SharedPreferences sharedPreferences = 
getActivity().getSharedPreferences("namefile", Context.MODE_PRIVATE); 
(use Shared Preferences in Fragment)
*/
Set<String> getListValueFromKey =
sharedPreferences.getStringSet("yourKey",new HashSet<String>());
getListValueFromKey.add("Hahaha");
getListValueFromKey.add("Kakaka");
getListValueFromKey.add("Hohoho");
/*  Add value or Do nothing …… */
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.clear(); //[important] Clearing your editor before using it.
editor.putStringSet("yourKey", getListValueFromKey);
editor.commit();

I have had exactly same problem like yours and this worked for me. For my whole code sample see

like image 124
Nguyễn Hoàng Thiên Phước Avatar answered Nov 10 '22 06:11

Nguyễn Hoàng Thiên Phước


I have a login screen and wanted the app to appear as if it's remained "logged in" at the internal screen after the app is closed/destroyed/phone call/etc.

I have a Preferences Object to save values following Login or Register. I read preference values in all the key screen onResume() methods.

After login (for example):

SharedPreferences app_preferences = PreferenceManager.getDefaultSharedPreferences(activity);
SharedPreferences.Editor editor = app_preferences.edit();
editor.putString("sessionId", application.currentSessionId);
editor.putString("userId", application.currentUserId);
editor.putString("userEmail", application.currentUserEmail);
editor.putString("siteUserId", application.currentSiteUserId);
editor.commit();

Within onResume() of Activities: (ie, within internal screens)

SharedPreferences app_preferences = PreferenceManager.getDefaultSharedPreferences(activity);
application.currentSessionId = app_preferences.getString("sessionId", "");
application.currentUserId = app_preferences.getString("userId", "");
application.currentUserEmail = app_preferences.getString("userEmail", "");
application.currentSiteUserId = app_preferences.getString("siteUserId", "");

Note. I have application "global" variables, ie, application.currentSessionId, you can just substitute your variables

Try something similar maybe your not saving or retrieving the values correctly because SharePreferences should work

like image 10
wired00 Avatar answered Nov 10 '22 06:11

wired00


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.

like image 1
Tigger Avatar answered Nov 10 '22 06:11

Tigger