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 |
float |
Retrieves a |
int |
Retrieves an |
long |
Retrieves a |
@Nullable String |
Retrieves a |
@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 |
void |
Sets a |
void |
Sets an |
void |
Sets a |
void |
Sets a |
void |
putStringSet(String key, @Nullable Set<String> values)Sets a set of |
Public constructors
Public methods
getBoolean
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
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
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
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
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 |
| See also | |
|---|---|
putString |
getStringSet
public @Nullable Set<String> getStringSet(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 |
| See also | |
|---|---|
putStringSet |
putBoolean
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
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
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
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
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
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 |