Android Shared Preferences




















Android SharedPreferences is useful when you want to save some data across all Activity.

Sometimes SharedPreferences is useful for login session.Once user successfully loggined in app then whenever user open app is already see as loggined user.it saves data in database and next time check if there are data then it display.

SharedPreferences is provide general framework  to save and retrieve persistent data in key-pairs value of primitives data type.

you can use  SharedPreferences to save any primitives data type like strings,ints,booleans,floats,longs.

You can create Object of SharedPreferences using two methods,

1).getSharedPreferences() : Using this methods you can create Multiple   SharedPreferences.and its first parameters in name of SharedPreferences.

2).getPreferences() : Using this method you can create Single SharedPreferences.


  
Now how to save,retrieve,remove or clear all data of SharedPreferences ?. 

Save Data ::
     // Create object of SharedPreferences.
     SharedPreferences sharedPref= getSharedPreferences("mypref", 0);
    //now get Editor
     SharedPreferences.Editor editor= sharedPref.edit();
    //put your value
     editor.putString("name", strName);
     editor.putString("pwd", strPass);
   //commits your edits
     editor.commit();
 
 Using putString(),putBoolean(),putInt(),putFloat(),putLong() you can save primitives data type.

 Don't forgot to commit SharedPreferences, Otherwise data is not saved.
 Retrieve Data ::

 SharedPreferences sharedPref= getSharedPreferences("mypref", 0);
String name = sharedPref.getString("name", "");
 String password = sharedPref.getString("pwd", "");

here you get data from SharedPreferences, you can check first time when your OnCreate() method is called and its never generates any exception even if data is inserted you just check value of your primitives data type.

Clear ALL Data ::

 SharedPreferences sharedPref= getSharedPreferences("mypref",0);
 SharedPreferences.Editor editor = sharedPref.edit();
 editor.clear();      //its clear all data.
 editor.commit();  //Don't forgot to commit  SharedPreferences.
 
Remove Single Data ::
 

 SharedPreferences sharedPref= getSharedPreferences("mypref",0);
 SharedPreferences.Editor editor = sharedPref.edit();
 editor.remove("name");//its remove name field from your SharedPreferences
 editor.commit(); //Don't forgot to commit  SharedPreferences.

Note : Its store data until you clear data from app or uninstall app.

Here i put keep me logged in Functionality Using Shared Preferences.

6 comments:

  1. thanks samir for the reply it saves the values in sharedpreferences and maintains the session but how do i clear the values in sharedpreferences when the user clicks on logout?

    ReplyDelete
    Replies
    1. SharedPreferences settings = getSharedPreferences(
      PREFRENCES_NAME, Context.MODE_PRIVATE);
      settings.edit().clear().commit();

      Delete
  2. hi, Thanks. What is the different between SaveInstanceState and SharedPreferences? How do I able to put the object into the SharedPreferences?

    ReplyDelete
  3. Really nice demo,
    thanks for posting Samir.

    ReplyDelete
  4. bagus sekali artikelnya,..... sangat bermanfaat,...



    By : development android jakarta | firzil.co.id

    ReplyDelete
  5. Thx a lot, really clear and straight forward!

    ReplyDelete

Android Testing App