Android SharedPreferences Tutorial and Example

In this Android tutorial we are going to see how to use Android SharedPreferences class to store and retrieve application specific persistent data.

Android SharedPreferences Tutorial

  • Android SharedPreferences allows us to store private primitive application data in the form of key-value pair.
  • Android stores shared preference settings as XML file in shared_prefs folder under DATA/data/[application package] directory. The DATA folder can be obtained by calling Environment.getDataDirectory() (usually it is /data).
  • SharedPreferences is application specific, i.e.) the data is lost when you perform one of the options,
    • once you uninstall the application
    • once you clear application data (through Settings)

How to get SharedPreferences instance?

To use shared preferences, you can use one of the following methods,

Method 1:

Use SharedPreferences getSharedPreferences (String name, int mode). This method gets shared preferences from a specified file.


  • PREFS_NAME is the name of the file.
  • Context.MODE_PRIVATE is the operating mode.

Other modes are,

Operating ModeConstant valueDescription
MODE_PRIVATE0File creation mode: the default mode, where the created file can only be accessed by the calling application.
MODE_WORLD_READABLE1This constant was deprecated in API level 17.
Creating world-readable files is very dangerous, and likely to cause security holes in applications.
MODE_WORLD_WRITEABLE2This constant was deprecated in API level 17.
Creating world-writable files is very dangerous, and likely to cause security holes in applications.
MODE_MULTI_PROCESS4This method will check for modification of preferences even if the sharedpreference instance has already been loaded
MODE_APPEND32768This will append the new preferences with the already exisiting preferences
MODE_ENABLE_WRITE_AHEAD_LOGGING8Database open flag. When it is set , it would enable write ahead logging by default

Method 2:

Gets a SharedPreferences instance that points to the default file that is used by the preference framework in the given context. Here the file is stored as

DATA/data/[application package name]/shared_prefs/[application package name]_preferences.xml


Store Data in SharedPreferences

To save a value in SharedPreferences, you can use SharedPreferences.Editor class.

  1. Get SharedPreferences instance using one of the methods explained above.
  2. Get SharedPreferences.Editor instance by calling edit() method in SharedPreferences instance.
  3. Store values by calling one of the putXXXX() methods.
  4. Commit the editor object.

Only primitive data types can be stored in SharedPreferences. Other methods are,

Retrieve Data from SharedPreferences

To get a value from shared preferences, you can use the SharedPreferences class’ getXXXX methods without the Editor object.

  • Get SharedPreferences instance using one of the methods explained above.
  • Call one of the getXXXX() methods using SharedPreferences instance.

Clear SharedPreferences Data

To remove all values from preferences use editor.clear() method as shown below.

To remove a specific key-value pair use editor.remove(KEY) method as shown below.

Android SharedPreferences Example

Project Description

In this Android Example, we will see how to use SharedPreferences to share data from one activity to another.

  • We create a separate SharedPreference utility class with methods to save, get, clear, remove from SharedPreferences.
  • We create two activities,
    • MainActivity – to save the value entered in EditText.
    • SecondActivity – to get the value from SharedPreferences and display it in TextView.

Download “Android SharedPreferences Demo” – Downloaded 6363 times – 2 MB

Android Project

Create a new Android project and name it as SharedPreferenceDemo.



Open res/values/strings.xml and edit to have the content as shown below.

Layout files

This XML layout file (activity_main.xml) defines an EditText and a Button which is ued by Open activity_main.xml file in res/layout and copy the following content.


This XML layout file (activity_second.xml) defines a TextView which is used by Create a new activity_second.xml file in res/layout and copy the following content.

Source files

SharedPreference class

Create a new class SharedPreference in the package com.androidopentutorials.sharedpreference.utils. This class defines methods to save, get and remove shared preferences values.

MainActivity class

Open class and copy the following code. This class gets value from EditText, stores it in SharedPreferences and starts the second activity.

SecondActivity class

Create a new class and copy the following code. This class gets the value from SharedPreferences and displays it in TextView.



When we go directly to second activity, no value is displayed as we have not yet stored the value in shared preference. Once the submit button is pressed, the value is saved in shared preference. Now this value will be shown on subsequent app launches by directly going to the second activity.


Storing Java objects in SharedPreferences

Android SharedPreferences allows you to store only primitive values or set of strings (java.util.Set). To store custom Java objects (object, arrays or lists) in SharedPreferences you can use one of the following methods,

  1. Generate toString() method in your class and save the object as String by calling toString()
    • This method is not useful when you want to retrieve the value and construct the object from String.
  2. Using external library such as GSon or Jackson to convert Java object to/from JSON (JavaScript Object Notation). After converting to JSON object, you can store it as string in SharedPreferences. Gson has methods,
    • toJson() – Convert Java object to JSON format
    • fromJson() – Convert JSON into Java object

To use Gson

Store custom Java object in SharedPreferences using Gson

Retrieve custom Java object from SharedPreferences using Gson

By using the above method, you can add a custom Java object, array or list to Android SharedPreferences.

Storing JSON object in SharedPreferences

To store JSONObject or JSONArray in SharedPreferences, use the toString() method and store as String in SharedPreferences.

To store JSONObject in SharedPreferences

To get JSONObject from SharedPreferences