2

I'm confused about exactly how SharedPreferences are managed by the Android operating system.

I'm coding an app that stores a Date value (serialized as a String) to the SharedPreferences when a user performs a certain action. Later, if this value is available, it is used to show the user how long ago the last instance of that action occurred.

Based on my reading, my understanding of SharedPreferences is the following:

  • As of 6.0, if allowBackup=true is set in the manifest, then the app's SharedPreferences will be stored in the user's Google account in the cloud, and restored whenever the user installs that app on a device.
  • This would imply that this does not occur prior to 6.0, or if allowBackup=false is set in the manifest.
  • Based on this question/answer, I would expect a reinstallation to preserve the SharedPreferences for a given app on a given device.

The problem I am running into is that, when installing from Android Studio directly on my LG Optimus G Pro (running 4.4.2), each installation seems to completely wipe out the SharedPreferences. I am simply executing a Run command, so I don't think the app is being uninstalled and re-installed, is it?

If the App Store update information is correct, then this isn't a big deal for my end users. It is, however, annoying when I am actively coding this feature and keep having to do the same action twice each time I make a tweak and rebuild.

So, my questions are two:

  1. Is there a better mechanism for doing what I'm trying to do than to use SharedPreferences? It seems like the best fit, but am I missing something?
  2. Is there a way to have SharedPreferences persist across rebuild/re-run sessions on a device when the app is installed from Android Studio? Is the issue because of using Kit Kat? (I don't have access to test on my Marshmallow device at the moment.)
Community
  • 1
  • 1
mbm29414
  • 11,558
  • 6
  • 56
  • 87
  • Have you tried this: http://stackoverflow.com/questions/9815363/in-android-is-there-any-way-to-preserve-sharedpreferences-after-an-uninstall ? – drawinfinity Mar 06 '17 at 16:05
  • I saw that, but I don't think that's the solution I want. That seems to be the solution if I wanted this for all of my end-users. I'm OK with them losing shared preferences if they uninstall the app. I just want to maintain SharedPreferences while I am developing the app. Does that make sense? – mbm29414 Mar 06 '17 at 16:08
  • All the data stored by using shared preferences is stored in a XML file. – HarshitMadhav Mar 06 '17 at 16:20
  • SharedPreferences do persist over reinstall/update. Probably there are some issues in your SharedPreferences handling. Maybe you could post relevant code? – Okas Mar 06 '17 at 16:28
  • So, I guess I should've used more Rubber Duck Problem Solving (https://blog.codinghorror.com/rubber-duck-problem-solving). In formatting my code to post here, I discovered I was using an uninitialized instance variable, which was causing my access of `SharedPreferences` to fail silently (per my design in the try/catch block). Using the appropriate method instead (which instantiates the instance variable when necessary) fixed the issue. Thanks! – mbm29414 Mar 07 '17 at 15:21

0 Answers0