7

I have a SharedPreference that counts the amount of launches of my App. I expect this to be 0, when I install my app. Nevertheless it is 14. The same strange behaviour I observe with my database, that already exists on a fresh install.

I didn't intent to recycle my app data (like in the Google Cloud). On my device in the account settings the app-data sync is on. If I turn it off, and make a reinstall I observe the same behaviour.

Anyone has every observed such a behaviour? Is there any way to prevent recycling old data and force a proper install?

Anthea
  • 3,741
  • 5
  • 40
  • 64
  • 6
    Could this be because of the "Auto Backup" feature introduced in Android Marshmallow? [https://developer.android.com/guide/topics/data/autobackup.html](https://developer.android.com/guide/topics/data/autobackup.html) – Mauin Jan 23 '17 at 14:46
  • that's it! A lot of hair pulling for a single line of code ... – Anthea Jan 23 '17 at 14:50
  • 1
    @Mauin make an answer and get credit – Anthea Jan 23 '17 at 15:07

3 Answers3

14

In Android Marshmallow Google introduced the "Auto Backup" feature which is turned on by default if your targetSdkVersion is >=23.

This will back up your database and SharedPreferences by default and restore it when you re-install the application. To turn this feature off you have to add android:allowBackup="false" to your AndroidManifest.xml.

More info here: Auto Backup for Apps

Mauin
  • 483
  • 3
  • 12
0

Review your code in AndroidManifest on tag application if it has android:allowBackup="false". If you don't have (by default it's true), your app participates on android backup and restore infrastructure and can happen exactly what you say.

More information in this post: What is "android:allowBackup"?

Community
  • 1
  • 1
Neonamu
  • 736
  • 1
  • 7
  • 21
0

This needs to be handled from app side regarding shared Prefs.

Created a shared preference helper class and in the helper class have the below condition.

private static String SHARED_PREFS_VERSION = "SharedPrefsVersion"; // In this save the version of current shared Prefs.

void SharedPrefsHelper() {

    if( BuildConfig.Version > getSharedPrefsVersion() ) {

        SharedPreferences.Editor editor = prefs.edit(); editor.clear(); // Clear all the shared Prefs and update the current version.

        SetSharedPrefsVersion(BuildConfig.Version);

    }
}

For further reference look at :

https://stackoverflow.com/a/12381061/7364024

Community
  • 1
  • 1
ChaitanyaAtkuri
  • 1,672
  • 11
  • 15