Android SharedPreferences Example

Here you will get android SharedPreferences example.

SharedPreferences in android is used to save data which is available across entire application and the data persists even after the application is closed. It is widely used for session management in android applications. SharedPreferences uses key-value pair to save and retrieve data.

 

Save Data

First of all we have to get SharedPreferences instance by calling getSharedPreferences() method. It can be done in following way.

 

Here first parameter is the user define key in string format and second parameter is the mode. The various modes that we can use are:

  • MODE_APPEND
  • MODE_ENABLE_WRITE_AHEAD_LOGGING
  • MODE_MULTI_PROCESS
  • MODE_PRIVATE
  • MODE_WORLD_READABLE
  • MODE_WORLD_WRITEABLE

 

Next step is to get SharedPreferences.Editor class object by calling edit() method using SharedPreferences object. It can be done in following way.

 

After that call putString() method using Editor object.

 

Here both key and value are in string format. There are some other methods like putBoolean(), putInt(), putLong() and putFloat() to save boolean, integer, long and float values respectively.

 

Finally call commit() method to save the changes.

 

Retrieve Data

First get instance of SharedPreferences by calling getSharedPreferences() method. After that use getString() method to fetch the data. It can be done in following way.

 

When there is no data associated with given key then the default value is returned. There are some other methods to fetch data, like getBoolean(), getInt(), getFloat() and getLong().

 

We can use contains() method to check whether SharedPreferences contain some value or not.

 

Modify Data

To modify any data, save the data using the same key that you used earlier. It will overwrite new data on previous data.

 

Delete Data

Use remove() method to delete a data associated with particular key. You can use clear() method to delete all the data present in SharedPreferences. It can be done in following way.

 

You must always call commit() method after doing any changes in data.

 

Below I have shared one simple example that will show how SharedPreferences is used in android.

 

Android SharedPreferences Example

Create a project with package name thecrazyprogrammer.androidexample. Now paste following code in respective files.

activity_main.xml

 

MainActivity.java

 

Run project and save some data using textbox and save button. Now close the application and run it again. You will see that the textbox display the data you have saved earlier. This proves that data is not lost even after closing the application. You can use remove button to delete the data.

Android SharedPreferences Example

 

Comment below if you have any doubts regarding above android SharedPreferences example.

Happy Coding!! 🙂 🙂

One thought on “Android SharedPreferences Example

Leave a Reply

Your email address will not be published. Required fields are marked *