# PreferenceDataStore

Artifact: [androidx.preference:preference](https://developer.android.com/jetpack/androidx/releases/preference) [View Source](https://cs.android.com/search?q=file:androidx/preference/PreferenceDataStore.java+class:androidx.preference.PreferenceDataStore) Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/preference#1.0.0)

*** ** * ** ***


```
public abstract class PreferenceDataStore
```

<br />

*** ** * ** ***

A data store interface to be implemented and provided to the `https://developer.android.com/reference/androidx/preference/Preference` framework. This can be used to replace the default `https://developer.android.com/reference/android/content/SharedPreferences.html`, if needed.

In most cases you want to use `https://developer.android.com/reference/android/content/SharedPreferences.html` as it is automatically backed up and migrated to new devices. However, providing custom data store to preferences can be useful if your app stores its preferences in a local database, cloud, or they are device specific like "Developer settings". It might be also useful when you want to use the preferences UI but the data is not supposed to be stored at all because they are only valid per session.

Once a put method is called it is the full responsibility of the data store implementation to safely store the given values. Time expensive operations need to be done in the background to prevent from blocking the UI. You also need to have a plan on how to serialize the data in case the activity holding this object gets destroyed.

By default, all "put" methods throw `https://developer.android.com/reference/java/lang/UnsupportedOperationException.html`.

| See also |
|---|---|
| `https://developer.android.com/reference/androidx/preference/Preference#setPreferenceDataStore(androidx.preference.PreferenceDataStore)` |   |
| `https://developer.android.com/reference/androidx/preference/PreferenceManager#setPreferenceDataStore(androidx.preference.PreferenceDataStore)` |   |

## Summary

| ### Public constructors |
|---|
| `https://developer.android.com/reference/androidx/preference/PreferenceDataStore#PreferenceDataStore()()` |

| ### Public methods |
|---|---|
| `boolean` | `https://developer.android.com/reference/androidx/preference/PreferenceDataStore#getBoolean(java.lang.String,boolean)(https://developer.android.com/reference/java/lang/String.html key, boolean defValue)` Retrieves a `https://developer.android.com/reference/java/lang/Boolean.html` value from the data store. |
| `float` | `https://developer.android.com/reference/androidx/preference/PreferenceDataStore#getFloat(java.lang.String,float)(https://developer.android.com/reference/java/lang/String.html key, float defValue)` Retrieves a `https://developer.android.com/reference/java/lang/Float.html` value from the data store. |
| `int` | `https://developer.android.com/reference/androidx/preference/PreferenceDataStore#getInt(java.lang.String,int)(https://developer.android.com/reference/java/lang/String.html key, int defValue)` Retrieves an `https://developer.android.com/reference/java/lang/Integer.html` value from the data store. |
| `long` | `https://developer.android.com/reference/androidx/preference/PreferenceDataStore#getLong(java.lang.String,long)(https://developer.android.com/reference/java/lang/String.html key, long defValue)` Retrieves a `https://developer.android.com/reference/java/lang/Long.html` value from the data store. |
| `@https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/java/lang/String.html` | `https://developer.android.com/reference/androidx/preference/PreferenceDataStore#getString(java.lang.String,java.lang.String)(https://developer.android.com/reference/java/lang/String.html key, @https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/java/lang/String.html defValue)` Retrieves a `https://developer.android.com/reference/java/lang/String.html` value from the data store. |
| `@https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/java/util/Set.html<https://developer.android.com/reference/java/lang/String.html>` | `https://developer.android.com/reference/androidx/preference/PreferenceDataStore#getStringSet(java.lang.String,java.util.Set<java.lang.String>)(https://developer.android.com/reference/java/lang/String.html key, @https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/java/util/Set.html<https://developer.android.com/reference/java/lang/String.html> defValues)` Retrieves a set of Strings from the data store. |
| `void` | `https://developer.android.com/reference/androidx/preference/PreferenceDataStore#putBoolean(java.lang.String,boolean)(https://developer.android.com/reference/java/lang/String.html key, boolean value)` Sets a `https://developer.android.com/reference/java/lang/Boolean.html` value to the data store. |
| `void` | `https://developer.android.com/reference/androidx/preference/PreferenceDataStore#putFloat(java.lang.String,float)(https://developer.android.com/reference/java/lang/String.html key, float value)` Sets a `https://developer.android.com/reference/java/lang/Float.html` value to the data store. |
| `void` | `https://developer.android.com/reference/androidx/preference/PreferenceDataStore#putInt(java.lang.String,int)(https://developer.android.com/reference/java/lang/String.html key, int value)` Sets an `https://developer.android.com/reference/java/lang/Integer.html` value to the data store. |
| `void` | `https://developer.android.com/reference/androidx/preference/PreferenceDataStore#putLong(java.lang.String,long)(https://developer.android.com/reference/java/lang/String.html key, long value)` Sets a `https://developer.android.com/reference/java/lang/Long.html` value to the data store. |
| `void` | `https://developer.android.com/reference/androidx/preference/PreferenceDataStore#putString(java.lang.String,java.lang.String)(https://developer.android.com/reference/java/lang/String.html key, @https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/java/lang/String.html value)` Sets a `https://developer.android.com/reference/java/lang/String.html` value to the data store. |
| `void` | `https://developer.android.com/reference/androidx/preference/PreferenceDataStore#putStringSet(java.lang.String,java.util.Set<java.lang.String>)(https://developer.android.com/reference/java/lang/String.html key, @https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/java/util/Set.html<https://developer.android.com/reference/java/lang/String.html> values)` Sets a set of `https://developer.android.com/reference/java/lang/String.html`s to the data store. |

## Public constructors

### PreferenceDataStore

Added in 1.0.0

```
public PreferenceDataStore()
```

## Public methods

### getBoolean

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/preference#1.0.0)

```
public boolean getBoolean(String key, boolean defValue)
```

Retrieves a `https://developer.android.com/reference/java/lang/Boolean.html` value from the data store.

| Parameters |
|---|---|
| `https://developer.android.com/reference/java/lang/String.html key` | The name of the preference to retrieve |
| `boolean defValue` | Value to return if this preference does not exist in the storage |

| Returns |
|---|---|
| `boolean` | the value from the data store or the default return value |

| See also |
|---|---|
| `https://developer.android.com/reference/androidx/preference/PreferenceDataStore#getBoolean(java.lang.String,boolean)` |   |

### getFloat

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/preference#1.0.0)

```
public float getFloat(String key, float defValue)
```

Retrieves a `https://developer.android.com/reference/java/lang/Float.html` value from the data store.

| Parameters |
|---|---|
| `https://developer.android.com/reference/java/lang/String.html key` | The name of the preference to retrieve |
| `float defValue` | Value to return if this preference does not exist in the storage |

| Returns |
|---|---|
| `float` | The value from the data store or the default return value |

| See also |
|---|---|
| `https://developer.android.com/reference/androidx/preference/PreferenceDataStore#putFloat(java.lang.String,float)` |   |

### getInt

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/preference#1.0.0)

```
public int getInt(String key, int defValue)
```

Retrieves an `https://developer.android.com/reference/java/lang/Integer.html` value from the data store.

| Parameters |
|---|---|
| `https://developer.android.com/reference/java/lang/String.html key` | The name of the preference to retrieve |
| `int defValue` | Value to return if this preference does not exist in the storage |

| Returns |
|---|---|
| `int` | The value from the data store or the default return value |

| See also |
|---|---|
| `https://developer.android.com/reference/androidx/preference/PreferenceDataStore#putInt(java.lang.String,int)` |   |

### getLong

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/preference#1.0.0)

```
public long getLong(String key, long defValue)
```

Retrieves a `https://developer.android.com/reference/java/lang/Long.html` value from the data store.

| Parameters |
|---|---|
| `https://developer.android.com/reference/java/lang/String.html key` | The name of the preference to retrieve |
| `long defValue` | Value to return if this preference does not exist in the storage |

| Returns |
|---|---|
| `long` | The value from the data store or the default return value |

| See also |
|---|---|
| `https://developer.android.com/reference/androidx/preference/PreferenceDataStore#putLong(java.lang.String,long)` |   |

### getString

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/preference#1.0.0)

```
public @Nullable String getString(String key, @Nullable String defValue)
```

Retrieves a `https://developer.android.com/reference/java/lang/String.html` value from the data store.

| Parameters |
|---|---|
| `https://developer.android.com/reference/java/lang/String.html key` | The name of the preference to retrieve |
| `@https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/java/lang/String.html defValue` | Value to return if this preference does not exist in the storage |

| Returns |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/java/lang/String.html` | The value from the data store or the default return value |

| See also |
|---|---|
| `https://developer.android.com/reference/androidx/preference/PreferenceDataStore#putString(java.lang.String,java.lang.String)` |   |

### getStringSet

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/preference#1.0.0)

```
public @Nullable Set<String> getStringSet(String key, @Nullable Set<String> defValues)
```

Retrieves a set of Strings from the data store.

| Parameters |
|---|---|
| `https://developer.android.com/reference/java/lang/String.html key` | The name of the preference to retrieve |
| `@https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/java/util/Set.html<https://developer.android.com/reference/java/lang/String.html> defValues` | Values to return if this preference does not exist in the storage |

| Returns |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/java/util/Set.html<https://developer.android.com/reference/java/lang/String.html>` | The values from the data store or the default return values |

| See also |
|---|---|
| `https://developer.android.com/reference/androidx/preference/PreferenceDataStore#putStringSet(java.lang.String,java.util.Set<java.lang.String>)` |   |

### putBoolean

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/preference#1.0.0)

```
public void putBoolean(String key, boolean value)
```

Sets a `https://developer.android.com/reference/java/lang/Boolean.html` value to the data store.

Once the value is set the data store is responsible for holding it.

| Parameters |
|---|---|
| `https://developer.android.com/reference/java/lang/String.html key` | The name of the preference to modify |
| `boolean value` | The new value for the preference |

| See also |
|---|---|
| `https://developer.android.com/reference/androidx/preference/PreferenceDataStore#getBoolean(java.lang.String,boolean)` |   |

### putFloat

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/preference#1.0.0)

```
public void putFloat(String key, float value)
```

Sets a `https://developer.android.com/reference/java/lang/Float.html` value to the data store.

Once the value is set the data store is responsible for holding it.

| Parameters |
|---|---|
| `https://developer.android.com/reference/java/lang/String.html key` | The name of the preference to modify |
| `float value` | The new value for the preference |

| See also |
|---|---|
| `https://developer.android.com/reference/androidx/preference/PreferenceDataStore#getFloat(java.lang.String,float)` |   |

### putInt

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/preference#1.0.0)

```
public void putInt(String key, int value)
```

Sets an `https://developer.android.com/reference/java/lang/Integer.html` value to the data store.

Once the value is set the data store is responsible for holding it.

| Parameters |
|---|---|
| `https://developer.android.com/reference/java/lang/String.html key` | The name of the preference to modify |
| `int value` | The new value for the preference |

| See also |
|---|---|
| `https://developer.android.com/reference/androidx/preference/PreferenceDataStore#getInt(java.lang.String,int)` |   |

### putLong

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/preference#1.0.0)

```
public void putLong(String key, long value)
```

Sets a `https://developer.android.com/reference/java/lang/Long.html` value to the data store.

Once the value is set the data store is responsible for holding it.

| Parameters |
|---|---|
| `https://developer.android.com/reference/java/lang/String.html key` | The name of the preference to modify |
| `long value` | The new value for the preference |

| See also |
|---|---|
| `https://developer.android.com/reference/androidx/preference/PreferenceDataStore#getLong(java.lang.String,long)` |   |

### putString

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/preference#1.0.0)

```
public void putString(String key, @Nullable String value)
```

Sets a `https://developer.android.com/reference/java/lang/String.html` value to the data store.

Once the value is set the data store is responsible for holding it.

| Parameters |
|---|---|
| `https://developer.android.com/reference/java/lang/String.html key` | The name of the preference to modify |
| `@https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/java/lang/String.html value` | The new value for the preference |

| See also |
|---|---|
| `https://developer.android.com/reference/androidx/preference/PreferenceDataStore#getString(java.lang.String,java.lang.String)` |   |

### putStringSet

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/preference#1.0.0)

```
public void putStringSet(String key, @Nullable Set<String> values)
```

Sets a set of `https://developer.android.com/reference/java/lang/String.html`s to the data store.

Once the value is set the data store is responsible for holding it.

| Parameters |
|---|---|
| `https://developer.android.com/reference/java/lang/String.html key` | The name of the preference to modify |
| `@https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/java/util/Set.html<https://developer.android.com/reference/java/lang/String.html> values` | The set of new values for the preference |

| See also |
|---|---|
| `https://developer.android.com/reference/androidx/preference/PreferenceDataStore#getStringSet(java.lang.String,java.util.Set<java.lang.String>)` |   |