T - Type of the value that can be persisted by this preference implementation.public abstract class SharedPreference<T> extends Object
SharedPreferences and later obtain such value.
This class provides API necessary to persist a desired preference value in SharedPreferences
via updateValue(Object) followed by putIntoPreferences(SharedPreferences) and
to obtain such persisted value from preferences via getFromPreferences(SharedPreferences).
The current value that has been obtained from shared preferences for a particular preference may
be obtained via getValue().
The key for each shared preference is required to be specified 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(String key,
T defValue)
Creates a new instance of SharedPreference with the given key and default value.
|
| Modifier and Type | Method and Description |
|---|---|
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.
|
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> |
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.public final String getKey()
SharedPreference(String, Object) constructors.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.