General settings reset data when the application is forcibly closed or the device restarts

I am developing an application in which I store username and password in SharedPreferences . Everything works fine for me, saving as well as retrieving values. But I found that when I restart the device or the application closes forcibly, the value stored in SharedPreferences is reset. And when I run my application again, I get null values ​​in SharedPreferences . Here is what I do to store 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 here is how I extract it:

 SharedPreferences emailLoginSP = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); loginEmail = emailLoginSP.getString("prefEmailId", null); loginUserId = emailLoginSP.getString("prefUserId", null); loginAccessToken = emailLoginSP.getString("prefAccess_token", null); 

Everything is still working. Again I declare my problem that I get zero values ​​when I force close or restart my device. Can we save it forever in the application memory? Or am I doing something wrong here?

Any help would be appreciated.

+5
source share
5 answers

I have a login screen and you want the application to appear as if it remained "logged in" on the internal screen after closing the application / destruction / phone call / etc.

I have a Preferences object to store the values ​​following Login or Register . I read preference values ​​in all onResume() screen 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(); 

Inside onResume () actions: (i.e. in 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 "global" application variables, i.e. application.currentSessionId , you can just replace your variables

Try something like this, you may not save or return values ​​correctly, because SharePreferences should work

+6
source

Hello! A solution that worked for me!

Decision 1 Decision 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 had the same problem as yours and it worked for me. For all my sample code, see

+4
source

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 has not been verified from memory, so an error may occur.

You want to minimize .commit() calls, so there is only one at the end.

pref_file_name can be anything you want, with lowercase letters, no numbers at startup, etc.

+1
source

See what I did as shown below.

  sharedPreferences = PreferenceManager .getDefaultSharedPreferences(getApplicationContext()); editor = sharedPreferences.edit(); isPaidVerison = sharedPreferences.getInt("isPaidVerison", 0); editor.putInt("isPaidVerison", 1); editor.commit(); 

And it works great. The data will remain in Sharedprefrences until you reinstall the application or until you clear the data.

And I get the data this way.

  isPaidVerison = sharedPreferences.getInt("isPaidVerison", 0); 
0
source

If you want to try a different approach than the general settings, you can use the file to write the values, and then extract the values ​​from it. Declare the text file emailLogin.txt and use it as follows to write data.

  try { Context context = MainActivity.this; OutputStreamWriter out= new OutputStreamWriter(context.openFileOutput(EMAILLOGIN, 0)); out.write(email_text); out.write("\r\n"); out.write(userIDToken); out.write("\r\n"); out.write(accessToken); out.write("\r\n"); out.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } 

After writing to a file, you can do the following to read it.

  File file = this.getFileStreamPath(EMAILLOGIN); if(file.exists()) { try { InputStream in=openFileInput(NUMBERS); if (in!=null) { InputStreamReader tmp=new InputStreamReader(in); BufferedReader reader=new BufferedReader(tmp); String str; String strcount[]= new String[20]; java.util.Arrays.fill(strcount, 0, 10, ""); while ((str = reader.readLine()) != null) { strcount[linecount]=str; } loginEmail = strcount[0]; loginUserId = strcount[1]; loginAccessToken = strcount[2]; in.close(); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } 

This ensures that even when your application closes or the device reboots, you can still read the required values ​​from the file.

0
source

All Articles