• Have Any Question?
  • contact@eitworld.com

Shared preferences





    • In android SharedReference is a class that provides a structure.
    • Using this class we can only store the primitive values such as int, long ,float etc and we have to use key value pair for this.
    • The data stored by us, is also available on the different user sessions.
    • To use the object of SharedReference class, there is two ways; we can call the getSharedReference() method and remember that we may use this method when we want to create more preference files.
    • And other option is that we can call the getPreference() method, using this method we can create only one preference file.
    • To do this, first of all we have to create the object of SharedPreference class and pass the variable_name and initial_value parameters as follows:
SharedPreferences myObj = new SharedPreferences(variable_name, initial_value);
    • After that we have to create the object of SharedPreference.Editor class to store the value.
    • To do this we have to call the edit() method using the object of SharedPreference class as follows:
SharedPreferences.Editor ediObj = myObj.edit();
    • To store the different values, android provides different methods such as if we want to store Boolean value then we have to use putBoolean() method and if we want to store string value then we have to use putString() method.
    • We can use these methods with the help of Editor class object and call these methods such as
ediObj.putString(“name”,value);
    • After that we have to call the commit() method to save the value as follows:
edtObj.commit();
  • To read the data, android provides the different methods for different kind of data such as if we want to get Boolean data then we have to use the getBoolean() method and if we want to get the string data then we have to use the getString() method.


Real Time Web Analytics