Skip to content

Storing and Accessing SharedPreferences

Christopher Clement edited this page Oct 31, 2021 · 20 revisions

Overview

Often you'll find it is necessary to store certain options persistently throughout the lifetime of the application. Using the SharedPreferences interface is the perfect way to do this! This tutorial will cover storing and accessing data using the SharedPreferences interface.

Storing Data with SharedPreferences

In order to store data to the SharedPreferences you need to first instantiate an instance of the SharedPreferences like so.

Specifying a Preference File

SharedPreferences sharedPreferences = getSharedPreferences("Settings", Context.MODE_PRIVATE);
val sharedPreferences = getSharedPreferences("Settings", Context.MODE_PRIVATE)

The string Settings is the name of the preference file you wish to access. If it does not exist, it will be created. The mode value of 0 designates the default behavior, which is to allow read access to only to the application. There are other read/write permissions that can be specified, but are no longer encouraged for security reasons.

Using a Default Preferences File

First, add the following dependency:

implementation 'androidx.preference:preference:1.1.1'

If you wish to have a common preference file and don't wish to specify a file, you can also use default shared preferences too:

SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(applicationContext)

Using this way will default the preference file to be stored as /data/data/com.package.name/shared_prefs/com.package.name_preferences.xml.

Editing Preferences

When the user interacts with a preference UI, e.g., an EditTextPreference and changes its default value, then the new value is stored in the default preference file. If there is a need to edit a preferences file without user interaction, it can be done by creating an Editor instance of SharedPreferences like so.

SharedPreferences.Editor editor = sharedPreferences.edit();
val editor: SharedPreferences.Editor = sharedPreferences.edit()

Then you can begin to add data to the Settings file declared when you instantiated the SharedPreferences like so.

int id = 1;
editor.putString("keyName", "newValue");
editor.putInt("id", id);
val id: Int = 1
editor.putString("keyName", "newValue")
editor.putInt("id", id)

Once you are finished adding data, you need to 'apply()' the edits by calling:

editor.apply();
editor.apply()

That's the last step. Your data is stored and you can then access your data using the method below.

Accessing Stored Data from SharedPrefrences

Once you have stored some data to your SharedPrefrences you may retrieve this value and others by using the following method.

First you need to instantiate an instance of your shared preferences.

SharedPreferences sharedPreferences = getSharedPreferences("Settings", Context.MODE_PRIVATE);
val sharedPreferences = getSharedPreferences("Settings", Context.MODE_PRIVATE)

The string Settings is the name of the settings file you wish to access. If it does not exist it will be created. The mode value of 0 designates the default behavior.

The final step is to access the data like so.

String setting = sharedPreferences.getString("keyName", "defaultValue");
val setting: String = sharedPreferences.getString("keyName", "defaultValue")

This will either grab the value that was previously set with the key of "keyName" or will return the string "defaultValue" if it is not found. That's all there is to it.

References

Finding these guides helpful?

We need help from the broader community to improve these guides, add new topics and keep the topics up-to-date. See our contribution guidelines here and our topic issues list for great ways to help out.

Check these same guides through our standalone viewer for a better browsing experience and an improved search. Follow us on twitter @codepath for access to more useful Android development resources.

Clone this wiki locally