Shared Preferences Reset Data When App Is Force Closed or Device Is Restarted

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

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

Shared Preferences Data resets when application is closed

tempScore=pref.getInt("key_name",0);
if(mScr>tempScore)
{
Editor editor = pref.edit();
editor.putInt("key_name", mScr);
editor.commit();
//tempScore=pref.getInt("key_name",0);
}
tempScore=pref.getInt("key_name",0);

This one workd

SharedPreferences being reset after force close

Ok I was able to solve this by removing the "protected static" from my save method.

Instead of calling a global save method, I simply placed the save method in each class that would need to save and then only call the save method in the onPause() and onDestroy() methods.

I noticed that if I called save() too many times within a class that also seemed to erase my SharedPreferences when I closed the app.

TIP:

Do not use static methods for getting or setting shared preferences

Shared Preferences reset data when app crashed. Please guide

So after the crash when it went to load the Preferences there was a blank in the preferences xml file which caused the preferences to reset.

To avoid this you could put all preference modifications in synchronized blocks or even use one synchronized static method for all preference writing.

I think - you need a better way of managing and storing the data you're saving.

The next time the shared preferences were accessed however, the xml file was cleared and started new.

for example :

private static final class SharedPreferencesImpl implements SharedPreferences {
...
public String getString(String key, String defValue) {
synchronized (this) {
String v = (String)mMap.get(key);
return v != null ? v : defValue;
}
}
...
public final class EditorImpl implements Editor {
public Editor putString(String key, String value) {
synchronized (this) {
mModified.put(key, value);
return this;
}
}
...
}
}

Shared Preferences reset when the app is force closed or crashes

After some digging around I finally discovered the source of the problem. Through an error in my code, a string preference was getting saved with a null key. So after the crash when it went to load the Preferences there was a blank in the preferences xml file which caused the preferences to crash and be reset. For some reason I was not getting the stack trace of the preference crash, only the immediate cause of the initial crash.

Just to add some more details in case someone else has a similar problem in the future:

The xml file that has the preferences stayed intact up to and through the force close button press. The next time the shared preferences were accessed however, the xml file was cleared and started anew.

SharedPreferences doesn't save on force close app

i fixed it by changing the field rememberEmail to a Boolean instead of a string containing a 1 or 0. I dont know why this didn't work but I'm happier about the fact that after 11 days of frustration this is no issue anymore.

Also did I delete all the preference files in folder /data/data/com.example/shared_prefs/

changed code in LoginActivity

// ....
private void loadUserCredentials() {
Boolean rememberEmail = appPreferences.getPreferenceBoolean(PREF_REMEMBER_EMAIL);
String email = appPreferences.getPreferenceString(PREF_EMAIL);
mEmailView = (EditText) findViewById(R.id.email);
mEmailView.setText(rememberEmail ? email : "");
cbRememberEmail = (CheckBox) findViewById(R.id.cbRememberEmail);
cbRememberEmail.setChecked(rememberEmail);
}
private void saveUserCredentials() {
try {
Boolean rememberEmail = ((CheckBox) findViewById(R.id.cbRememberEmail)).isChecked();
appPreferences.setPreferenceBoolean(PREF_REMEMBER_EMAIL, rememberEmail);
String email = (rememberEmail) ? mEmail : "";
appPreferences.setPreferenceString(PREF_EMAIL, email);
}
// ....

and in AppPreferences

// ....
private String getKey(String key) {
key = key.toLowerCase();
return LOGIN_CREDENTIALS + "." + key;
}
// newly added methods below
public String getPreferenceBoolean(String key) {
return settings.getBoolean(getKey(key), DEFAULT_BOOLEAN);
}
public void setPreferenceBoolean(String key, Boolean value) throws Exception {
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean(getKey(key), (Boolean) value);
editor.commit();
}
// ....

I hope you won't make the same mistake as I did ;)



Related Topics



Leave a reply



Submit