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.

Upvotes: 1

Views: 398

Answers (3)

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.

Upvotes: 1

Harish Penta
Harish Penta

Reputation: 500

Its because the boolean value by default sets its value to false .. it only takes the true value when there is empty value returned from the shared preference , but boolean value must have a value either true or false...it doesn't support null value.

so what you need to do is set the boolean value of the session to true programatically in the class before loading and then get the value from the session

Upvotes: 1

user5484179
user5484179

Reputation:

I can't commnet to your question because of less points So, if you're 100 sure that you didn't set any value to FIRST_LAUNCH, then 1. try to clean the app's data 2. Can you please provide the full source code

And @miller what if there are more than one sharedpreferences

Upvotes: 1

Related Questions