Home > Enterprise >  What is the use of String Key in putBoolean() method?
What is the use of String Key in putBoolean() method?

Time:02-01

what is the use of "notifications" in the underlined part? I had given many different strings in the place of "notifications" (underlined part) and the code runs without any issue. So what is the string "notifications" doing here exactly?

Refer to this image down.

https://i.stack.imgur.com/RxZ2p.jpg

CodePudding user response:

This is basically an example of a key value pair and it will be coming up again and again in your android development career.

What a key value pair means is ..

Imagine a box with a key. Now you put some value in the box and only that particular key ( read value) can open the box.

So sharedpreferences.edit().putBoolean("notifications",false).apply creates a box in memory with the key "notifications".

So when you need to retrieve the value you need to put the key in the appropriate place like

boolean notificationValue = getSharedPreferences(sharedpreferences, Context.MODE_PRIVATE).getBoolean("notification", true);

CodePudding user response:

It is the name of the sharedpreference value you are assigning. Later when you want to get the value say in another activity you should do something like this

 boolean notificationValue = getSharedPreferences(sharedpreferences, Context.MODE_PRIVATE).getBoolean("notification", true);
  •  Tags:  
  • Related