Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 782
huey77 Avatar asked Jan 06 '23 00:01

huey77


2 Answers

To fix, I put this in my AndroidManifest:

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

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

like image 136
Dakota Jay Whipple Avatar answered Jan 07 '23 13:01

Dakota Jay Whipple


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

like image 27
huey77 Avatar answered Jan 07 '23 14:01

huey77