PreferenceDataStore

public abstract class PreferenceDataStore


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

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

Summary

Public constructors

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.

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

Retrieves a String value from the data store.

@Nullable Set<String>
getStringSet(String key, @Nullable 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

Sets a String value to the data store.

void

Sets a set of Strings to the data store.

Public constructors

PreferenceDataStore

Added in 1.0.0
public PreferenceDataStore()

Public methods

getBoolean

Added in 1.0.0
public boolean getBoolean(String key, boolean defValue)

Retrieves a Boolean value from the data store.

Parameters
String 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
getBoolean

getFloat

Added in 1.0.0
public float getFloat(String key, float defValue)

Retrieves a Float value from the data store.

Parameters
String 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
putFloat

getInt

Added in 1.0.0
public int getInt(String key, int defValue)

Retrieves an Integer value from the data store.

Parameters
String 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
putInt

getLong

Added in 1.0.0
public long getLong(String key, long defValue)

Retrieves a Long value from the data store.

Parameters
String 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
putLong

getString

Added in 1.0.0
public @Nullable String getString(String key, @Nullable String defValue)

Retrieves a String value from the data store.

Parameters
String key

The name of the preference to retrieve

@Nullable String defValue

Value to return if this preference does not exist in the storage

Returns
@Nullable String

The value from the data store or the default return value

See also
putString

getStringSet

Added in 1.0.0
public @Nullable Set<StringgetStringSet(String key, @Nullable Set<String> defValues)

Retrieves a set of Strings from the data store.

Parameters
String key

The name of the preference to retrieve

@Nullable Set<String> defValues

Values to return if this preference does not exist in the storage

Returns
@Nullable Set<String>

The values from the data store or the default return values

See also
putStringSet

putBoolean

Added in 1.0.0
public 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
String key

The name of the preference to modify

boolean value

The new value for the preference

See also
getBoolean

putFloat

Added in 1.0.0
public 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
String key

The name of the preference to modify

float value

The new value for the preference

See also
getFloat

putInt

Added in 1.0.0
public 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
String key

The name of the preference to modify

int value

The new value for the preference

See also
getInt

putLong

Added in 1.0.0
public 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
String key

The name of the preference to modify

long value

The new value for the preference

See also
getLong

putString

Added in 1.0.0
public void putString(String key, @Nullable String value)

Sets a String value to the data store.

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

Parameters
String key

The name of the preference to modify

@Nullable String value

The new value for the preference

See also
getString

putStringSet

Added in 1.0.0
public void putStringSet(String key, @Nullable 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
String key

The name of the preference to modify

@Nullable Set<String> values

The set of new values for the preference

See also
getStringSet