Question

So I am still a bit new to java/android and I am attempting to read a preference value, but I am trying to understand each part about it so it makes a little more sense to me...

SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
boolean soundEnabledPref = sharedPref.getBoolean("sound", soundEnabled);

in the getBoolean, I am trying to understand what the first string is "sound", and what the soundEnabled boolean is for.

so I have declared soundEnabled up top as a boolean, i am guessing this is what stores the value that I am retrieving?

what is the purpose of the first string though "sound"? I want to make sure I am naming it correctly if its being used to store something, or if I am referencing something with it then I probably need to name it something different.

I have read through the developer documentation and still am a bit confused so if someone could just give me a quick explanation a bit more broken down I would really appreciate it!

Était-ce utile?

La solution

  • "sound" is the key, under which you are looking for the value
  • soundEnabled is a boolean variable, which has some value - true or false; this value will be used as default value for soundEnabledPref in case there is no "sound" key; it can be written like that also:

    boolean soundEnabledPref = sharedPref.getBoolean("sound", true);
    

    If there is no "sound" key, soundEnabledPref will take the default value of true

A good practice for keys is to use constants - you WILL avoid simple typo errors later down the line, eg.:

public static final String KEY_SOUND = "sound";

Then

boolean soundEnabledPref = sharedPref.getBoolean(ClassNameWhereItsDefined.KEY_SOUND, true);

Autres conseils

"sound" is the key corresponding to which you will have a value stored in your SharedPreferences.

This is described in the docs as well.

In simpler terms, you can consider it as a variable which will hold a value.

Shared Preference using Dictionary concept it Key , Value pair as dictionary it save any key with string and value as object shared preference is safe type , when you want save integer you using method putInt or putBoolean to save boolean , and when you retrieve from shared preference also return for you strong type after it cast value for you ,

hope it helpful for you

You can store several things in shared preferences, so you use keys to identify what values to set and what values to retrieve later on.

In this case, sound is your key.

At some point in your app, you called SharedPreferences.Editor.putBoolean("sound", myBoolean);.

At some point later in your app you call SharedPreferences.getBoolean("sound", defaultBoolean); to find out what the value of myBoolean was. In this call, defaultBoolean is simply the default value you want to get if by any chances you never called SharedPreferences.Editor.putBoolean and therefore a value for "sound" does not exist.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top