Home » Android » android – User settings saved in SharedPreferences removed or lost between reloads of app

android – User settings saved in SharedPreferences removed or lost between reloads of app

Posted by: admin June 15, 2020 Leave a comment

Questions:

My app stores simple settings in SharedPreferences it works fine.
However for one person who’s downloaded my app is having problems.
The settings in the SharedPreferences are getting lost between closing and reloading the app.

Could he have a permissions problem somewhere on his phone that’s preventing the data from being saved between sessions?

Has anyone experienced this or know of any reason why this could be happening?
I’m having a pretty hard time debugging it, I don’t know where to start.

// I'm using SharedPreferences Like so:
prefs = getSharedPreferences(this.getString(R.string.prefs_name), 0);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("accounts", accounts);
editor.commit();

//retrieving stored information like:
SharedPreferences prefs = getSharedPreferences(this.getString(R.string.prefs_name), 0);
String accounts = prefs.getString("accounts","[]");
How to&Answers:

We are experienced same problems with our Android apps. Our userbase is pretty big (several million users) and by our statistics subjected problems occured for about 0,2% – 0,3% of users. It seems to be not so much, but with our userbase it thousands of users.

After long search for fixes of this problem, we’ve made a decision to stop using SharedPreferences for our projects. We are using simple SQLiteDatabase instead, and it works very well.

Answer:

I had same problem. Fortunately, I had access to the device and it helped me to find problem. First of all, I have studied log file and found error:

W/SharedPreferencesImpl(31354): org.xmlpull.v1.XmlPullParserException: Map value without name attribute: string

So, preferences file was corrupted in some way. I have made preferences file world-accessible in my application:

SharedPreferences prefs = context.getSharedPreferences("main", Context.MODE_WORLD_READABLE);

Then I pull the file from device to computer

adb pull data/data/my.package.name/shared_prefs/main.xml c:\main.xml

and check preferences file content:

<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map>
<string>Paris,France</string>
<string name="HideStatusBar">1</string>

First parameter has no “name” attribute. I have checked a code and found that in some circumstances first parameters was written in follow way:

 SharedPreferences.Editor e = _Prefs.edit();
 e.putString(null, paramValue);
 e.commit()     

Name was null. I have fixed the error and problem has disappeared. So, trivial error can completely corrupt preferences file.

Answer:

You should put out an update to your app that temporarily saves, clears, and recreates the preferences file.

I had a similar situation. Some users had not only their preferences file messed up, but also their SQL database. You can’t really ask people to delete and reinstall, they may lose data. But your app can automatically back it up first, remove the corrupted files, and then put it all back.