huey77
huey77

Reputation: 683

SharedPreferences.getBoolean returns true for default value

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

Upvotes: 5

Views: 4177

Answers (3)

Dakota Jay Whipple
Dakota Jay Whipple

Reputation: 550

To fix, I put this in my AndroidManifest:

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

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

Upvotes: 11

huey77
huey77

Reputation: 683

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

Upvotes: 1

FerDensetsu
FerDensetsu

Reputation: 746

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?

Upvotes: 0

Related Questions