Zachary Powell

Software Programmer

Zachary Powell

Download my CV Send me a Message

Using SharedPreferences in Android to store data

Zachary Powell - 06/12/2015

Shared Preferences are a great way to store primitive data types across user sessions.

What does this mean? Using Shared Preferences, you can save some form of data (string, boolean, integer…) in a way that means your app can retrieve that data at any point even after the app has been exited. This, as you might have guessed, is designed to allow you to save settings that the user might set within your app and that is the example we will use.

Shared Preferences are very simple to use. With just a couple of methods you can set and get your saved data with no need to worry about files or parsing the data. In the background, Shared Preferences are actually stored in a file within your Applications data directory. As such, it is important to note that while a normal user would not have access to this file, if the users device is rooted, they could access and manually edit this file. It is not recommended to use this for storing if a user has unlocked a paid features for example.

Setting a Shared Preference

Setting and getting a shared preference are very easy tasks, but it makes sense to set before we get.

First we create a SharedPreference object and initialise it as below:

SharedPreferences sharedPref = getSharedPreferences("MyPref", Context.MODE_PRIVATE);

getSharedPreferences takes two arguments, the first a string. This is the name of our preference file – if this file does not already exist it will be created. The second argument is an integer, which defines the operating mode. Possible values are:

  • Context.MODE_PRIVATE (value 0) – The preference file can only be accessed by the application that created it. This is the default operation mode.
  • Context.MODE_WORLD_WRITEABLE (value 2) – The preference file can be written to by all applications. This mode was deprecated in API 17 due to the clear security issues
  • Context.MODE_WORLD_READABLE (value 1) – The preference file can be read by all applications. Again this mode was deprecated in API 17

So we now have a SharedPerference object to work with, but to set and save a preference, we need to get a Shared Preference editor, set the required Preference and then save it so that its written to our preference file.

sharedPref.edit().putBoolean("show_background", false).apply();

In the above example, we first get our editor with .edit() then call the .put() method. There is a method for each supported data type – see HERE for the complete list. Our example is setting a boolean data type so we use .putBoolean().

These methods take two arguments, the first is a String. This is the key used to identify the Preference and we will use this to retrieve the data. The second argument is the data itself .putBoolean() takes a Boolean.

Finally we call .apply() on this to actually save the Preference to the file.

Getting a Shared Preference

As I said before, getting a Shared Preference is just as easy (actually easier!). Again, we retrieve our preference file, just as we did before.

SharedPreferences sharedPref = getSharedPreferences("MyPref", Context.MODE_PRIVATE);

Then we can access any preference we have saved to this file. In the below example we access the “show_background” preference.

Boolean background = sharedPref.getBoolean("show_background", true);

Here we use the .getBoolean() method. Each data type has a method – for the complete list see HERE. These methods all take two arguments. The first is a String which is the key for the Preference we are getting and the second is the default value to be returned if the Preference is not found.

This second argument is important as it allows us to use the method before the user may have actually set the preference. For example, when the user first launches the application.

And that is it all you need to know about using Shared Preferences! If you have any questions, please leave them in the comments below.


Leave a Comment

Leave a Reply

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

Tags: , ,

“ Any fool can write code that a computer can understand. Good programmers write code that humans can understand. ”

-Martin Fowler