Search code examples
androidsharedpreferencesandroid-sharedpreferences

Shared preferences isn't gettingBoolean properly


I'm trying to load boolean value from Shared Preferences. On first launch it should be TRUE as I have never saved it to Shared Preferences. However, I'm somehow getting false.

See code below.

settings = context.getSharedPreferences(SAVED_PREFERENCES, 0);
isFirstLaunch = settings.getBoolean(FIRST_LAUNCH, true);
Log.d(TAG, "loadIsFirstLaunch: " + isFirstLaunch);

p.s. I haven't saved FIRST_LAUNCH value in shared preferences before 100%.

UPD1 I've double checked with empty project - code is working properly. Shared Preferences use Boolean class, not boolean primitive type. So it isn't the case that boolean by default is false. Also according to debug my shared preferences somehow includes FIRST_LAUNCH value by the time it is launched. So looking at how it got there.

UPD2 I finally stuck. Removed all mentionings of FirstLaunch, but it still somehow appears in SharedPreferences when another call to read other values is made. So question is - how can I get rid of saved value in Shared preference. http://take.ms/Rr0Xf

UPD3 I've changed name to my saved preferences file and it worked. So problem was that SOMEHOW device was keeping info for saved preferences even after application clean install. PFM.


Solution

  • So finally I found the problem and solution.

    In android M and above versions Google keeps application backups in google driver, that's why even after reinstall shared preferences were restoring. I've disabled backups.

    project manifest file under Application section set android:allowBackup="true" to false.