SharedPreferences.getBoolean returns true for default value
Asked Answered
N

3

5

For some reason, this returns true every time for the default value. I uninstalled my app and re-installed and i do the following to initialize the values. But for some reason the boolean is set to true instead of false.

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(LoadingActivity.this);
    final int locationType = prefs.getInt(Constants.PREFS_LOCATION, 0);
    final boolean skipWhatsNew = prefs.getBoolean(Constants.PREFS_DONT_SHOW_WHATS_NEW, false);

    Log.v("loading activity : " , " " + skipWhatsNew);

I'm not really sure why its doing this since it used to just set the value to false before.

I already looked at similar questions but none of them helped, so any help would be nice, thanks.

Questions that i have looked at already,

Android - SharedPreference.getBoolean retrieving false even if i am storing true?

SharedPreferences.getBoolean returns true everytime

The above code is also the first time i access this, and from what i understand, it should set to false since it is being created for the first time. (first answer on this question mentions this, unless I'm wrong)

android default values for shared preferences

Navelwort answered 29/11, 2016 at 23:20 Comment(10)
Possible duplicate of SharedPreferences.getBoolean returns true everytimeGerah
You need find and check track for key 'Constants.PREFS_DONT_SHOW_WHATS_NEW' , where are setting true for this, may be you are set value before get in some default config!Berkshire
I already looked at that question. The solution to that question is what I'm doing when i do getBoolean(string, false) . Thanks. @GerahNavelwort
It isn't being set anywhere. The code above is the first time I access it. From what i understand it should set the default value to false since it hasn't been created. The first answer on this question says that as well, so that is why I'm confused. #17754300 @ArfanMirzaNavelwort
Constants.PREFS_DONT_SHOW_WHATS_NEW. Please dont use your own constants in your post. How do we know whats in it? Just use a string literal so you post reproducable code.Ellon
It's a string. Knowing that its a string is all that matters. Thanks... @EllonNavelwort
No. And you are not serious.Ellon
Ok. Please explain to me why knowing the exact string is important?.. @EllonNavelwort
It looks as if you really dont want help. Well ok.Ellon
These comments are unproductive. I am also experiencing this issue. Tried clearing all data, uninstalling app, etc. The first time it is getting accessed, it returns true. I set the preference to false, uninstall app, and it returns true. If I set it to false, close app, then open again, then the preference is set to false.Vampire
V
10

To fix, I put this in my AndroidManifest:

<application
    android:allowBackup="false"
    ...>

See: https://developer.android.com/guide/topics/data/backup.html

Vampire answered 22/1, 2017 at 22:58 Comment(3)
I will have to try this next time i have this issue. Apparently whenever you delete the app and uninstall, it won't delete your cached data. So i had to clear that for it to reset to its default setting. So once i set it to true, it was true until i reset/cleared the cache.Navelwort
Had this same problem during debugging. Exactly what I needed, thanks!Faxun
Add the following to application tag if it results in Gradle build error: tools:replace="allowBackup". Details on: https://mcmap.net/q/2029561/-android-allowbackup-quot-true-quot-error-on-quot-true-quotSatanism
N
1

I was not able to figure out why it is returning true as the default.

So the only solution i found was the change the string in the first parameter of

getBoolean(Constants.PREFS_DONT_SHOW_WHATS_NEW, false);

I pretty much changed this string by one character, ie "string" to "string_"

Now it returns the default value of false. I have then tested the preference by changing it to true in other sections of my app and then restarting the app and it is still true. I then un-install the app and it is reset back to false as its default whenever i do the above code.

Don't know why this has happened. If anyone has any idea, please let me know.

Thanks

Navelwort answered 30/11, 2016 at 15:24 Comment(1)
For me also the same problem I got still, I have tried many things but nothing will happen.I used your tricks now showing first-time default value false.Veteran
P
0

You should paste the code where you're setting the value to false. There is no reason for this method to return true if you're explicitly setting it to false and also passing false as default value, there should be no way possible. Maybe there's a method where you're applying some kind of logic that makes it possible to return true, or maybe you're not using commit method after seeting the value.

Have you checked this questions yet?

SharedPreferences.getBoolean returns true everytime Android - SharedPreference.getBoolean retrieving false even if i am storing true?

Priming answered 29/11, 2016 at 23:47 Comment(1)
The code above is the first time i access it. From what I understand is that it should set the default value to false, which is the second parameter. My thought is that it should set to false since it hasn't been created anywhere. The first answer on this question also says the same, this is why I'm confused. #17754300Navelwort

© 2022 - 2024 — McMap. All rights reserved.