T - Type of the value that can be persisted by this preference implementation.public abstract class SharedPreference<T> extends Object
SharedPreferences and then retrieve it.
This class implements API necessary to persist its current value into SharedPreferences
via updateValue(Object) followed by save(SharedPreferences) and to retrieve such
persisted value from preferences via retrieve(SharedPreferences) followed by getValue().
The key for shared preference must be passed via SharedPreference(String, Object) constructor
and may be later obtained via getKey().
| Modifier and Type | Class and Description |
|---|---|
static interface |
SharedPreference.PreferenceChangeCallback<P>
Callback that may be used to listen for changes in value of a specific
SharedPreference
implementation. |
| Modifier | Constructor and Description |
|---|---|
protected |
SharedPreference(int keyResId,
T defaultValue)
Deprecated.
Use
SharedPreference(String, Object) instead. |
protected |
SharedPreference(String key,
T defValue)
Creates a new instance of SharedPreference with the given key and default value.
|
| Modifier and Type | Method and Description |
|---|---|
SharedPreference<T> |
attachKey(android.content.res.Resources resources)
Deprecated.
Due to dropped support of keys specified via resource ids, this method becomes obsolete.
|
android.content.SharedPreferences.OnSharedPreferenceChangeListener |
createOnChangeListener(SharedPreference.PreferenceChangeCallback<T> callback)
Creates a new instance of
SharedPreferences.OnSharedPreferenceChangeListener which
may be used to listen for changes in value of this preference within shared preferences. |
T |
getDefaultValue()
Returns the default value of this preference.
|
String |
getKey()
Returns the key of this preference.
|
int |
getKeyRes()
Deprecated.
Use
getKey() instead. |
T |
getValue()
Returns the value which is at this time being hold by this preference object.
|
protected abstract T |
onGetFromPreferences(android.content.SharedPreferences preferences)
Invoked to retrieve the actual value of this preference from the given shared preferences.
|
protected abstract boolean |
onPutIntoPreferences(android.content.SharedPreferences preferences)
Invoked to persist the actual value into the given shared preferences.
|
boolean |
putIntoPreferences(android.content.SharedPreferences preferences)
Persists the actual value of this preference into the given shared preferences.
|
SharedPreference<T> |
retrieve(android.content.SharedPreferences preferences)
Deprecated.
Use
getFromPreferences(SharedPreferences) instead. |
boolean |
save(android.content.SharedPreferences preferences)
Deprecated.
Use
putIntoPreferences(SharedPreferences) instead. |
SharedPreference<T> |
updateValue(T newValue)
Updates the actual value of this preference to the given one.
|
protected SharedPreference(String key, T defValue)
key - The key under which will be the value of this preference mapped within
shared preferences.defValue - Default value of this preference to return in case, when there is no value
saved withing shared preference yet.IllegalArgumentException - If the specified key is empty.@Deprecated protected SharedPreference(int keyResId, T defaultValue)
SharedPreference(String, Object) instead.Creates a new instance of SharedPreference with the given key resource and default value.
keyResId - Resource id of the desired key under which will be the value of this preference
mapped within shared preferences.defaultValue - Default value of this preference for case, when there is no value saved withing
shared preference yet.IllegalArgumentException - If the specified keyResId is invalid.attachKey(Resources)public final String getKey()
SharedPreference(String, Object) constructors.@Deprecated public final int getKeyRes()
getKey() instead.Return the resource id of the key of this preference.
SharedPreference(int, Object) constructor.@Deprecated public SharedPreference<T> attachKey(android.content.res.Resources resources)
Attaches the key for this preference from the given resources using the current key's resource
id specified via SharedPreference(int, Object).
resources - An application's resources to obtain key.IllegalArgumentException - If key obtained from the resources for the resource id is empty.public android.content.SharedPreferences.OnSharedPreferenceChangeListener createOnChangeListener(SharedPreference.PreferenceChangeCallback<T> callback)
SharedPreferences.OnSharedPreferenceChangeListener which
may be used to listen for changes in value of this preference within shared preferences.
Note, that the created listener will delegate the change event the given callback only in case when the key of changed value matches the key of this preference.
callback - Callback to be invoked when the value of this preference changes.SharedPreferences.registerOnSharedPreferenceChangeListener(SharedPreferences.OnSharedPreferenceChangeListener),
SharedPreferences.unregisterOnSharedPreferenceChangeListener(SharedPreferences.OnSharedPreferenceChangeListener)public final T getDefaultValue()
SharedPreference(String, Object)public SharedPreference<T> updateValue(T newValue)
Note, that calling this method does not trigger persisting of the given value into shared preferences.
In order to immediately persist the given new value of this preference into shared preferences,
this call need to be followed by putIntoPreferences(SharedPreferences).
newValue - New value for this preference.getValue()public final boolean putIntoPreferences(android.content.SharedPreferences preferences)
preferences - The instance of shared preferences where should be the actual value of
this preference persisted.True if put has been successful, false otherwise.getFromPreferences(SharedPreferences),
updateValue(Object),
getValue()protected abstract boolean onPutIntoPreferences(android.content.SharedPreferences preferences)
preferences - The instance of shared preferences into which should be the current value
of this preference persisted.True if put has been successful, false otherwise.public final T getValue()
Note, that this does not need to be necessarily the same value as persisted (if) within
shared preferences for this preference if the actual value has been just updated via
updateValue(Object) and not persisted via putIntoPreferences(SharedPreferences)
or getFromPreferences(SharedPreferences) was not called for this preference yet.
In order to always obtain the actual value of this preference from shared preferences use
getFromPreferences(SharedPreferences) instead.
protected abstract T onGetFromPreferences(android.content.SharedPreferences preferences)
preferences - The instance of shared preferences into which was the value of this preference
before saved.@Deprecated public SharedPreference<T> retrieve(android.content.SharedPreferences preferences)
getFromPreferences(SharedPreferences) instead.Retrieves the current value of this preference from the given shared preferences.
preferences - The instance of shared preferences into which was the value of this preference
before saved.getValue()@Deprecated public boolean save(android.content.SharedPreferences preferences)
putIntoPreferences(SharedPreferences) instead.Saves the current value of this preference into the given shared preferences.
preferences - The instance of shared preferences into which will be the current value of
this preference saved.True if saving operation succeed, false otherwise.updateValue(Object)