10

I have noticed that my app's SharedPreferences are completely reset if the app is force closed or is killed by the system for some reason. Obviously, yes, I should avoid having a crash in the first place but it does happen. If it does happen, the user shouldn't loss all of their settings and other data stored with the Preferences.

I found this thread on Google Groups but they couldn't solve it.

Anyone have any insight into why or how this happens and if there is a way to prevent it?

Also, fyi I have seen this on a 2.1 and a 2.2 device. It will happen on every crash, not just some of them.

Thanks

cottonBallPaws
  • 21,220
  • 37
  • 123
  • 171

1 Answers1

10

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.

cottonBallPaws
  • 21,220
  • 37
  • 123
  • 171
  • 1
    Thanks for the follow-up. I got bit by this as well. Not easy to detect until this prompted me to look for a null key that I left behind somehow. – mmeyer Dec 01 '11 at 22:10
  • @littleFluffykitty : does sqlite data also get cleared on force close? – Ashwin Oct 04 '12 at 11:46
  • I had the same kind of issue. And used files for storing data. It will not clear/delete, when app crash. Also, I loaded the sharedpref in Application onCreate after reading from this file. – jrh Jan 25 '14 at 17:40
  • i digged thousends of codes for the error and find your answer. and same issue :) thank you.I solved this after one day because of a null key. – Enes Oct 04 '14 at 17:16
  • 1
    How is this a solution? It doesn't address how to fix the issue. – Treewallie Sep 04 '19 at 15:31