Shared data is reset on application crash. Please direct

My app crashed and all data in shared preferences was cleared. I keep multiple flags and keep the user session in general preference. One of the IsFirstLaunch flags that tells me if the application is launched for the first time or not, if it returns true, then I load some data from the server and store it in a SQLite database .

I'm sorry, thanks in advance.

+3


source to share


1 answer


So after a crash when it loaded the settings, there was a space in the preference xml file which caused the preference to be reset.

To avoid this, you can put all preference modifications in synchronized blocks, or even use one synchronized static method for all preference entries .

I think you need a better way to manage and store the data you save.



The next time the shared preference was accessed, the xml file was cleaned up and started a new one.

eg:

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;
            }
        }
    ...
    }
}

      

0


source







All Articles