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=trueis set in the manifest, then the app'sSharedPreferenceswill 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=falseis set in the manifest. - Based on this question/answer, I would expect a reinstallation to preserve the
SharedPreferencesfor 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:
- 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? - Is there a way to have
SharedPreferencespersist 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.)