PreferenceDataStore
public
abstract
class
PreferenceDataStore
extends Object
java.lang.Object | |
↳ | androidx.preference.PreferenceDataStore |
A data store interface to be implemented and provided to the Preference
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 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
.
See also:
Summary
Public constructors | |
---|---|
PreferenceDataStore()
|
Public methods | |
---|---|
boolean
|
getBoolean(String key, boolean defValue)
Retrieves a |
float
|
getFloat(String key, float defValue)
Retrieves a |
int
|
getInt(String key, int defValue)
Retrieves an |
long
|
getLong(String key, long defValue)
Retrieves a |
String
|
getString(String key, String defValue)
Retrieves a |
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 |
void
|
putFloat(String key, float value)
Sets a |
void
|
putInt(String key, int value)
Sets an |
void
|
putLong(String key, long value)
Sets a |
void
|
putString(String key, String value)
Sets a |
void
|
putStringSet(String key, Set<String> values)
Sets a set of |
Inherited methods | |
---|---|
Public constructors
PreferenceDataStore
public PreferenceDataStore ()
Public methods
getBoolean
public 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 |
See also:
getFloat
public 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 |
See also:
getInt
public 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 |
See also:
getLong
public 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 |
See also:
getString
public 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 |
See also:
getStringSet
public 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 |
See also:
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 | |
---|---|
key |
String : The name of the preference to modify |
value |
boolean : The new value for the preference |
See also:
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 | |
---|---|
key |
String : The name of the preference to modify |
value |
float : The new value for the preference |
See also:
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 | |
---|---|
key |
String : The name of the preference to modify |
value |
int : The new value for the preference |
See also:
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 | |
---|---|
key |
String : The name of the preference to modify |
value |
long : The new value for the preference |
See also:
putString
public 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 |
See also:
putStringSet
public void putStringSet (String key, Set<String> values)
Sets a set of String
s 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 |
See also: