Save the date! Android Dev Summit is coming to Mountain View, CA on November 7-8, 2018.
added in version 26.1.0
belongs to Maven artifact com.android.support:preference-v7:28.0.0-alpha1

PreferenceDataStore

public abstract class PreferenceDataStore
extends Object

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


A data store interface to be implemented and provided to the Preferences framework. This can be used to replace the default SharedPreferences, if needed.

In most cases you want to use SharedPreferences 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.

Summary

Public constructors

PreferenceDataStore()

Public methods

boolean getBoolean(String key, boolean defValue)

Retrieves a Boolean value from the data store.

float getFloat(String key, float defValue)

Retrieves a Float value from the data store.

int getInt(String key, int defValue)

Retrieves an Integer value from the data store.

long getLong(String key, long defValue)

Retrieves a Long value from the data store.

String getString(String key, String defValue)

Retrieves a String value from the data store.

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

Retrieves a set of Strings from the data store.

void putBoolean(String key, boolean value)

Sets a Boolean value to the data store.

void putFloat(String key, float value)

Sets a Float value to the data store.

void putInt(String key, int value)

Sets an Integer value to the data store.

void putLong(String key, long value)

Sets a Long value to the data store.

void putString(String key, String value)

Sets a String value to the data store.

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

Sets a set of Strings to the data store.

Inherited methods

From class java.lang.Object

Public constructors

PreferenceDataStore

added in version 26.1.0
PreferenceDataStore ()

Public methods

getBoolean

added in version 26.1.0
boolean getBoolean (String key, 
                boolean defValue)

Retrieves a Boolean value from the data store.

Parameters
key String: the name of the preference to retrieve

defValue boolean: 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

getFloat

added in version 26.1.0
float getFloat (String key, 
                float defValue)

Retrieves a Float value from the data store.

Parameters
key String: the name of the preference to retrieve

defValue float: 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

getInt

added in version 26.1.0
int getInt (String key, 
                int defValue)

Retrieves an Integer value from the data store.

Parameters
key String: the name of the preference to retrieve

defValue int: 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

getLong

added in version 26.1.0
long getLong (String key, 
                long defValue)

Retrieves a Long value from the data store.

Parameters
key String: the name of the preference to retrieve

defValue long: 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

getString

added in version 26.1.0
String getString (String key, 
                String defValue)

Retrieves a String value from the data store.

Parameters
key String: the name of the preference to retrieve

defValue String: value to return if this preference does not exist in the storage

Returns
String the value from the data store or the default return value

getStringSet

added in version 26.1.0
Set<String> getStringSet (String key, 
                Set<String> defValues)

Retrieves a set of Strings from the data store.

Parameters
key String: the name of the preference to retrieve

defValues Set: values to return if this preference does not exist in the storage

Returns
Set<String> the values from the data store or the default return values

putBoolean

added in version 26.1.0
void putBoolean (String key, 
                boolean value)

Sets a Boolean value to the data store.

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

Parameters
key String: the name of the preference to modify

value boolean: the new value for the preference

putFloat

added in version 26.1.0
void putFloat (String key, 
                float value)

Sets a Float value to the data store.

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

Parameters
key String: the name of the preference to modify

value float: the new value for the preference

putInt

added in version 26.1.0
void putInt (String key, 
                int value)

Sets an Integer value to the data store.

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

Parameters
key String: the name of the preference to modify

value int: the new value for the preference

putLong

added in version 26.1.0
void putLong (String key, 
                long value)

Sets a Long value to the data store.

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

Parameters
key String: the name of the preference to modify

value long: the new value for the preference

putString

added in version 26.1.0
void putString (String key, 
                String value)

Sets a String value to the data store.

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

Parameters
key String: the name of the preference to modify

value String: the new value for the preference

putStringSet

added in version 26.1.0
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.

Parameters
key String: the name of the preference to modify

values Set: the set of new values for the preference