8

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","[]");
user229044
  • 232,980
  • 40
  • 330
  • 338
Rob
  • 7,039
  • 4
  • 44
  • 75
  • are you using defaultSharedPreferences? if not did you make sure the string key you used is the same between uses? It is hard to know what you are doing wrong without a code snippet. – Tom Fobear May 10 '11 at 20:08
  • hey, no just SharedPreferences, I'm using the same keys through out, the app works fine for thousands, just this one guy. The rest of the app which relies on this information functions perfectly until he closes the app. – Rob May 10 '11 at 20:12
  • Could it be a storage space issue? – OceanBlue May 10 '11 at 20:18
  • Interesting, why didn't I think of that, I'll see what he says. Ta – Rob May 10 '11 at 20:22
  • @Rob did you ever get to the bottom of this? One of my users is reporting exactly the same problem. – Graham Borland Oct 25 '11 at 19:52
  • @GrahamBorland I never resolved this issue. Because it was only one person, I had to put it down to something specific on their phone possible issue with an addon or something, space was not the issue. All I could do was apologise and refund them. Sorry I can't be more help. – Rob Oct 26 '11 at 12:47
  • I also have this issue with an app in the market. Works fine for thousands, but on some phones the settings won't "stick" – thijs Jan 08 '12 at 20:41
  • I also have a similar issue on a app Im debugging, but I can not find a solution or a logical explination. You may set a bounty on this question. – Magakahn Jul 08 '12 at 13:24

3 Answers3

8

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.

HitOdessit
  • 7,198
  • 4
  • 36
  • 59
  • 1
    Thanks for your input. I have been also been using SQLiteDatabase in my apps. SharedPreferences cannot be trusted. – Rob Oct 10 '12 at 10:17
  • 1
    Accepting answer despite it not explaining the issues with SharedPreferences, it is the only reasonable approach to work around this broken behaviour with SharedPreferences. – Rob Oct 10 '12 at 10:21
8

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.

dvpublic
  • 657
  • 8
  • 8
0

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.

Alex
  • 5,909
  • 2
  • 35
  • 25