Here, I am gonna explore coding, Debugging, Error fixing, My articles and other technical posts. So stay tuned.

What is SharedPreferences in Android Studio | All about SharedPreferences in Android. | How to use SharedPreferences in android app

No comments

SharedPreference

So welcome Guy to my blog. I explain everything as much as possible to me by using images.
So lets get started


SharedPreference is just a simple mechanism to store data. It stores data in Key-Value pairs.

In the data there is something on the right side is Value and something on the left side is Key. 
So store that some data in the form of key and value. and to access some value we need to know about key. we can access any value with the help of key.We can store data like Username, Password,apptheme etc.




Type of data We can store 

  • Ints
  • floats
  • booleans
  • Long
  • Strings
  • Double 



Some uses of SharedPreferences

  • we can check for update or status of last update. Simply we can stored the time and date of the file
  • Remembering the user name and password or we can say the login information
  • Remember User settings.
  • Location Caching.



How to use it .

First, How to store data in it.

STEP-1

Get a reference to the SharedPreferences objact:

use : GetSharedPreferences(String name , int mode);
So here now what String name and int mode is. String name is for the name of your file name. like when we store data in SharedPrefereances it create a file in XML automatically in the app file. So we need to give the name of that file So that we can call that file with its name and we can access the data from that file. And the int mode is who can access your this file there are many modes in your android system like MODE_PRIVATE which means only you app can access that file, MODE_WORLD_READABLE Which means all the apps can read that file, MODE_WORLD_WRITEABLE all apps can write that file etc
for Example : 
SharedPrefereances SP= GetSharedPreferences("Data",Context.MODE_PRIVATE);

By doing this what will happens is that there is a file will be created in your android system with name Data.xml So this is first step After then yes we need to edit that file we need to add some values in it so then.

STEP-2

use: SharedPreferences.Editor edit=SP.edit();

In this We create the object of editor class with name edit and call the method edit() with the object of  SharedPreferences whis is SP in my Example.

SharedPreferences.Editor edit=SP.edit();

STEP-3

Then we need to add some values to our file. so call the method putString() by useing the object of Editor. like in my Example i used edit.

putString(Key, value);

As i said before that we can store any vale in it . like int, float,String etc 

edit.putString("name","Hemunt Sharma");
edit.putString("password","abc");


STEP-4

And in the last we call Commit() method to write these values into that Data file.

edit.Commit();






NowHow to Receive data from it.

STEP-1

Again create the object of SharedPreferences and give the file name in String and int mode
use: GetSharedPreferences(String name , int mode);
for Example : 
SharedPrefereances SP= GetSharedPreferences("Data",Context.MODE_PRIVATE);


STEP-2

Now time to get some values for the file Data.xml which we have created. Call the function getString().

use: getString(key, default value); 

for Example : 
SP.getString("name", "");
SP.getString("password", "");

Here the default value means of there is no value in any key then it will show up the default value.
Now store that value in any variable and and you can access that value

for Example :

String name = SP.getString("name", "");
String Password = SP.getString("password", "");



You can use there variables anywhere.So thats it for this one guys thank you for watching.



So thats all for  this one guys. if you want Video example on this topic then comment below.
and don,t forget to share this with your other friend who needs this kind of stuff more.


No comments :

Post a Comment