Loading and Saving Persistent Preferences

## Introduction

In this tutorial, you will learn how to save variables to persistent preferences and how to load them on other instances. You won't need to work with the filesystem, as Android can manage saved variables by itself.

## Dependencies

No special dependencies are needed for this tutorial.

## Importing

Import the following classes:

```
import android.preference.PreferenceManager;
import android.content.SharedPreferences;

```

## Initializing the SharedPreferences and Editor

You can start up a SharedPreference inside a method, or create a reuseable one. Declare a SharedPreferences variable:

```
SharedPreferences settings;

```

Initialize the variable by calling the method getDefaultSharedPreferences() from a PreferenceManager.

```
settings = PreferenceManager.getDefaultSharedPreferences(this);

```

Declare an Editor variable:

```
Editor editor;

```

## Saving a preference

For saving or loading preferences, android searches for values using String keys. Setup a string that will name our preference. In this tutorial, we will call it PREFERENCE\_TUTORIAL.

```
String preferenceKey = "PREFERENCE\_TUTORIAL";

```

Setup the Editor to edit the preferences with the edit() method.

```
editor = settings.edit();

```

Now you will input whichever values you like. Android differentiates some kinds of variables, so you won't have to go through the trouble of parsing Strings. Below are listed some of the supported methods:

```
editor.putBoolean(booleanKey, booleanValue);
editor.putFloat(floatKey, floatValue);
editor.putInt(intKey, intValue);
editor.putLong(longKey, longValue);
editor.putString(stringKey, stringValue);

```

For this tutorial, we will store an Integer called preferenceInt.

```
editor.putInt(preferenceKey, preferenceInt);

```

You can save more variables at the same time. When you are finished, you have two methods to save changes. The first one is commit(), that will run in the main thread, and the last one is apply(), that will run asynchronously in the background.

```
editor.commit(); // Runs in main thread
editor.apply(); // Runs in background

```

If it will not affect other methods of your application, always choose to run in the background. But if you need it to save instantly, you should choose commit().

When you are finished, your code should look like this:

```
String preferenceKey = "PREFERENCE\_TUTORIAL";
editor = settings.edit();
editor.putInt(preferenceKey, preferenceInt);
editor.commit();

```

## Loading a Preference

After you saved the preference keys, you can load them whenever you want, even after you close your application. Put methods for loading preferences in the onCreate() method to keep inputed data persistent through instances.

To load a preference, redeclare the key you want.

```
String preferenceKey = "PREFERENCE\_TUTORIAL";

```

Now you can use the SharedPreferences to get the values saved. You can use methods that are similar to the ones commented in the previous section. The first parameter is the String key. The second parameter is a default value, in case it doesn't find our key. This is useful for first runs or when the value is corrupted.

```
int preferenceInt = settings.getInt(preferenceKey, 100);

```

When you are finished, your code should look like this:

```
String preferenceKey = "PREFERENCE\_TUTORIAL";
int preferenceInt = settings.getInt(preferenceKey, 100);

```

## Conclusion

Your application can now load and save any preferences. This doesn't replace the use of a PreferenceSettings activity.

You now know how to save variables to the preferences and how to load them anytime.