# PreferenceDataStore

added in[version 26.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  
belongs to Maven artifact com.android.support:preference-v7:28.0.0-alpha1  
Summary:[Ctors](https://developer.android.com/reference/android/support/v7/preference/PreferenceDataStore#pubctors)\|[Methods](https://developer.android.com/reference/android/support/v7/preference/PreferenceDataStore#pubmethods)\|[Inherited Methods](https://developer.android.com/reference/android/support/v7/preference/PreferenceDataStore#inhmethods)\|[\[Expand All\]](https://developer.android.com/reference/android/support/v7/preference/PreferenceDataStore#)  

# PreferenceDataStore

| This package is part of the Android[support library](https://developer.android.com/topic/libraries/support-library)which is no longer maintained. The support library has been superseded by[AndroidX](https://developer.android.com/jetpack/androidx)which is part of[Jetpack](https://developer.android.com/jetpack). We recommend using the AndroidX libraries in all new projects. You should also consider[migrating](https://developer.android.com/jetpack/androidx/migrate)existing projects to AndroidX. To find the AndroidX class that maps to this deprecated class, see the AndroidX support library[class mappings](https://developer.android.com/jetpack/androidx/migrate/class-mappings).

`
public


abstract
class
PreferenceDataStore
`  
`

extends Object


``


`

|---|---------------------------------------------------|
| java.lang.Object                                     ||
| ↳ | android.support.v7.preference.PreferenceDataStore |

<br />

*** ** * ** ***

A data store interface to be implemented and provided to the Preferences framework. This can be used to replace the default[SharedPreferences](https://developer.android.com/reference/android/content/SharedPreferences.html), if needed.

In most cases you want to use[SharedPreferences](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 db, 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 are not supposed to be stored at all because they are valid per session only.

Once a put method is called it is 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[UnsupportedOperationException](https://developer.android.com/reference/java/lang/UnsupportedOperationException).  
**See also:**

- [setPreferenceDataStore(PreferenceDataStore)](https://developer.android.com/reference/android/support/v7/preference/Preference#setPreferenceDataStore(android.support.v7.preference.PreferenceDataStore))
- [setPreferenceDataStore(PreferenceDataStore)](https://developer.android.com/reference/android/support/v7/preference/PreferenceManager#setPreferenceDataStore(android.support.v7.preference.PreferenceDataStore))

## Summary

|                                                              ### Public constructors                                                              ||
|------------------------------------------------------------------------------------------------------------------------------------------------|---|
| ` `[PreferenceDataStore](https://developer.android.com/reference/android/support/v7/preference/PreferenceDataStore#PreferenceDataStore())`() ` |

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

| ### Inherited methods |
|-----------------------|---|
| [![](https://developer.android.com/static/assets/images/styles/disclosure_down.png)](https://developer.android.com/reference/android/support/v7/preference/PreferenceDataStore#)From class` java.lang.Object ` |-------------------|-------------------------------| | ` Object`         | ` clone() `                   | | ` boolean`        | ` equals(Object arg0) `       | | ` void`           | ` finalize() `                | | ` final Class<?>` | ` getClass() `                | | ` int`            | ` hashCode() `                | | ` final void`     | ` notify() `                  | | ` final void`     | ` notifyAll() `               | | ` String`         | ` toString() `                | | ` final void`     | ` wait(long arg0, int arg1) ` | | ` final void`     | ` wait(long arg0) `           | | ` final void`     | ` wait() `                    | ||

## Public constructors

### PreferenceDataStore

added in[version 26.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  

```
PreferenceDataStore ()
```

<br />

<br />

## Public methods

### getBoolean

added in[version 26.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  

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

Retrieves a[Boolean](https://developer.android.com/reference/java/lang/Boolean)value from the data store.

<br />

|                                           Parameters                                           ||
|------------|------------------------------------------------------------------------------------|
| `key`      | `String`: the name of the preference to retrieve <br />                            |
| `defValue` | `boolean`: value to return if this preference does not exist in the storage <br /> |

|                                   Returns                                   ||
|-----------|------------------------------------------------------------------|
| `boolean` | the value from the data store or the default return value <br /> |

**See also:**

- [getBoolean(String, boolean)](https://developer.android.com/reference/android/support/v7/preference/PreferenceDataStore#getBoolean(java.lang.String, boolean))  

### getFloat

added in[version 26.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  

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

Retrieves a[Float](https://developer.android.com/reference/java/lang/Float)value from the data store.

<br />

|                                          Parameters                                          ||
|------------|----------------------------------------------------------------------------------|
| `key`      | `String`: the name of the preference to retrieve <br />                          |
| `defValue` | `float`: value to return if this preference does not exist in the storage <br /> |

|                                  Returns                                  ||
|---------|------------------------------------------------------------------|
| `float` | the value from the data store or the default return value <br /> |

**See also:**

- [putFloat(String, float)](https://developer.android.com/reference/android/support/v7/preference/PreferenceDataStore#putFloat(java.lang.String, float))  

### getInt

added in[version 26.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  

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

Retrieves an[Integer](https://developer.android.com/reference/java/lang/Integer)value from the data store.

<br />

|                                         Parameters                                         ||
|------------|--------------------------------------------------------------------------------|
| `key`      | `String`: the name of the preference to retrieve <br />                        |
| `defValue` | `int`: value to return if this preference does not exist in the storage <br /> |

|                                 Returns                                 ||
|-------|------------------------------------------------------------------|
| `int` | the value from the data store or the default return value <br /> |

**See also:**

- [putInt(String, int)](https://developer.android.com/reference/android/support/v7/preference/PreferenceDataStore#putInt(java.lang.String, int))  

### getLong

added in[version 26.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  

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

Retrieves a[Long](https://developer.android.com/reference/java/lang/Long)value from the data store.

<br />

|                                         Parameters                                          ||
|------------|---------------------------------------------------------------------------------|
| `key`      | `String`: the name of the preference to retrieve <br />                         |
| `defValue` | `long`: value to return if this preference does not exist in the storage <br /> |

|                                 Returns                                  ||
|--------|------------------------------------------------------------------|
| `long` | the value from the data store or the default return value <br /> |

**See also:**

- [putLong(String, long)](https://developer.android.com/reference/android/support/v7/preference/PreferenceDataStore#putLong(java.lang.String, long))  

### getString

added in[version 26.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  

```
String getString (String key, 
                String defValue)
```

Retrieves a[String](https://developer.android.com/reference/java/lang/String)value from the data store.

<br />

|                                          Parameters                                           ||
|------------|-----------------------------------------------------------------------------------|
| `key`      | `String`: the name of the preference to retrieve <br />                           |
| `defValue` | `String`: value to return if this preference does not exist in the storage <br /> |

|                                  Returns                                   ||
|----------|------------------------------------------------------------------|
| `String` | the value from the data store or the default return value <br /> |

**See also:**

- [putString(String, String)](https://developer.android.com/reference/android/support/v7/preference/PreferenceDataStore#putString(java.lang.String, java.lang.String))  

### getStringSet

added in[version 26.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  

```
Set<String> getStringSet (String key, 
                Set<String> defValues)
```

Retrieves a set of Strings from the data store.

<br />

|                                          Parameters                                          ||
|-------------|---------------------------------------------------------------------------------|
| `key`       | `String`: the name of the preference to retrieve <br />                         |
| `defValues` | `Set`: values to return if this preference does not exist in the storage <br /> |

|                                      Returns                                      ||
|---------------|--------------------------------------------------------------------|
| `Set<String>` | the values from the data store or the default return values <br /> |

**See also:**

- [ERROR(/#putStringSet(String, Set))](https://developer.android.com/)  

### putBoolean

added in[version 26.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  

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

Sets a[Boolean](https://developer.android.com/reference/java/lang/Boolean)value to the data store.

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

<br />

|                           Parameters                           ||
|---------|-------------------------------------------------------|
| `key`   | `String`: the name of the preference to modify <br /> |
| `value` | `boolean`: the new value for the preference <br />    |

**See also:**

- [getBoolean(String, boolean)](https://developer.android.com/reference/android/support/v7/preference/PreferenceDataStore#getBoolean(java.lang.String, boolean))  

### putFloat

added in[version 26.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  

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

Sets a[Float](https://developer.android.com/reference/java/lang/Float)value to the data store.

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

<br />

|                           Parameters                           ||
|---------|-------------------------------------------------------|
| `key`   | `String`: the name of the preference to modify <br /> |
| `value` | `float`: the new value for the preference <br />      |

**See also:**

- [getFloat(String, float)](https://developer.android.com/reference/android/support/v7/preference/PreferenceDataStore#getFloat(java.lang.String, float))  

### putInt

added in[version 26.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  

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

Sets an[Integer](https://developer.android.com/reference/java/lang/Integer)value to the data store.

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

<br />

|                           Parameters                           ||
|---------|-------------------------------------------------------|
| `key`   | `String`: the name of the preference to modify <br /> |
| `value` | `int`: the new value for the preference <br />        |

**See also:**

- [getInt(String, int)](https://developer.android.com/reference/android/support/v7/preference/PreferenceDataStore#getInt(java.lang.String, int))  

### putLong

added in[version 26.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  

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

Sets a[Long](https://developer.android.com/reference/java/lang/Long)value to the data store.

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

<br />

|                           Parameters                           ||
|---------|-------------------------------------------------------|
| `key`   | `String`: the name of the preference to modify <br /> |
| `value` | `long`: the new value for the preference <br />       |

**See also:**

- [getLong(String, long)](https://developer.android.com/reference/android/support/v7/preference/PreferenceDataStore#getLong(java.lang.String, long))  

### putString

added in[version 26.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  

```
void putString (String key, 
                String value)
```

Sets a[String](https://developer.android.com/reference/java/lang/String)value to the data store.

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

<br />

|                           Parameters                           ||
|---------|-------------------------------------------------------|
| `key`   | `String`: the name of the preference to modify <br /> |
| `value` | `String`: the new value for the preference <br />     |

**See also:**

- [getString(String, String)](https://developer.android.com/reference/android/support/v7/preference/PreferenceDataStore#getString(java.lang.String, java.lang.String))  

### putStringSet

added in[version 26.1.0](https://developer.android.com/topic/libraries/support-library/revisions)  

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

Sets a set of Strings to the data store.

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

<br />

|                            Parameters                            ||
|----------|--------------------------------------------------------|
| `key`    | `String`: the name of the preference to modify <br />  |
| `values` | `Set`: the set of new values for the preference <br /> |

**See also:**

- [ERROR(/#getStringSet(String, Set))](https://developer.android.com/)  
-

  ## Interfaces

  - [DialogPreference.TargetFragment](https://developer.android.com/reference/android/support/v7/preference/DialogPreference.TargetFragment)
  - [Preference.OnPreferenceChangeListener](https://developer.android.com/reference/android/support/v7/preference/Preference.OnPreferenceChangeListener)
  - [Preference.OnPreferenceClickListener](https://developer.android.com/reference/android/support/v7/preference/Preference.OnPreferenceClickListener)
  - [PreferenceFragmentCompat.OnPreferenceDisplayDialogCallback](https://developer.android.com/reference/android/support/v7/preference/PreferenceFragmentCompat.OnPreferenceDisplayDialogCallback)
  - [PreferenceFragmentCompat.OnPreferenceStartFragmentCallback](https://developer.android.com/reference/android/support/v7/preference/PreferenceFragmentCompat.OnPreferenceStartFragmentCallback)
  - [PreferenceFragmentCompat.OnPreferenceStartScreenCallback](https://developer.android.com/reference/android/support/v7/preference/PreferenceFragmentCompat.OnPreferenceStartScreenCallback)
  - [PreferenceGroup.PreferencePositionCallback](https://developer.android.com/reference/android/support/v7/preference/PreferenceGroup.PreferencePositionCallback)
  - [PreferenceManager.OnDisplayPreferenceDialogListener](https://developer.android.com/reference/android/support/v7/preference/PreferenceManager.OnDisplayPreferenceDialogListener)
  - [PreferenceManager.OnNavigateToScreenListener](https://developer.android.com/reference/android/support/v7/preference/PreferenceManager.OnNavigateToScreenListener)
  - [PreferenceManager.OnPreferenceTreeClickListener](https://developer.android.com/reference/android/support/v7/preference/PreferenceManager.OnPreferenceTreeClickListener)
-

  ## Classes

  - [CheckBoxPreference](https://developer.android.com/reference/android/support/v7/preference/CheckBoxPreference)
  - [DialogPreference](https://developer.android.com/reference/android/support/v7/preference/DialogPreference)
  - [DropDownPreference](https://developer.android.com/reference/android/support/v7/preference/DropDownPreference)
  - [EditTextPreference](https://developer.android.com/reference/android/support/v7/preference/EditTextPreference)
  - [EditTextPreferenceDialogFragmentCompat](https://developer.android.com/reference/android/support/v7/preference/EditTextPreferenceDialogFragmentCompat)
  - [ListPreference](https://developer.android.com/reference/android/support/v7/preference/ListPreference)
  - [ListPreferenceDialogFragmentCompat](https://developer.android.com/reference/android/support/v7/preference/ListPreferenceDialogFragmentCompat)
  - [MultiSelectListPreferenceDialogFragmentCompat](https://developer.android.com/reference/android/support/v7/preference/MultiSelectListPreferenceDialogFragmentCompat)
  - [Preference](https://developer.android.com/reference/android/support/v7/preference/Preference)
  - [Preference.BaseSavedState](https://developer.android.com/reference/android/support/v7/preference/Preference.BaseSavedState)
  - [PreferenceCategory](https://developer.android.com/reference/android/support/v7/preference/PreferenceCategory)
  - [PreferenceDataStore](https://developer.android.com/reference/android/support/v7/preference/PreferenceDataStore)
  - [PreferenceDialogFragmentCompat](https://developer.android.com/reference/android/support/v7/preference/PreferenceDialogFragmentCompat)
  - [PreferenceFragmentCompat](https://developer.android.com/reference/android/support/v7/preference/PreferenceFragmentCompat)
  - [PreferenceGroup](https://developer.android.com/reference/android/support/v7/preference/PreferenceGroup)
  - [PreferenceManager](https://developer.android.com/reference/android/support/v7/preference/PreferenceManager)
  - [PreferenceManager.PreferenceComparisonCallback](https://developer.android.com/reference/android/support/v7/preference/PreferenceManager.PreferenceComparisonCallback)
  - [PreferenceManager.SimplePreferenceComparisonCallback](https://developer.android.com/reference/android/support/v7/preference/PreferenceManager.SimplePreferenceComparisonCallback)
  - [PreferenceScreen](https://developer.android.com/reference/android/support/v7/preference/PreferenceScreen)
  - [PreferenceViewHolder](https://developer.android.com/reference/android/support/v7/preference/PreferenceViewHolder)
  - [R](https://developer.android.com/reference/android/support/v7/preference/R)
  - [R.anim](https://developer.android.com/reference/android/support/v7/preference/R.anim)
  - [R.attr](https://developer.android.com/reference/android/support/v7/preference/R.attr)
  - [R.bool](https://developer.android.com/reference/android/support/v7/preference/R.bool)
  - [R.color](https://developer.android.com/reference/android/support/v7/preference/R.color)
  - [R.dimen](https://developer.android.com/reference/android/support/v7/preference/R.dimen)
  - [R.drawable](https://developer.android.com/reference/android/support/v7/preference/R.drawable)
  - [R.id](https://developer.android.com/reference/android/support/v7/preference/R.id)
  - [R.integer](https://developer.android.com/reference/android/support/v7/preference/R.integer)
  - [R.layout](https://developer.android.com/reference/android/support/v7/preference/R.layout)
  - [R.string](https://developer.android.com/reference/android/support/v7/preference/R.string)
  - [R.style](https://developer.android.com/reference/android/support/v7/preference/R.style)
  - [R.styleable](https://developer.android.com/reference/android/support/v7/preference/R.styleable)
  - [SeekBarPreference](https://developer.android.com/reference/android/support/v7/preference/SeekBarPreference)
  - [SwitchPreferenceCompat](https://developer.android.com/reference/android/support/v7/preference/SwitchPreferenceCompat)
  - [TwoStatePreference](https://developer.android.com/reference/android/support/v7/preference/TwoStatePreference)