Type - A type of the value that can this implementation of preference hold and manage its
saving/obtaining.public abstract class SharedPreference<Type> extends Object
PreferencesManager to manage
simplified storing and retrieving values from shared preferences.
This class implements API necessary to save its current value into SharedPreferences
via updateValue(Object) followed by save(SharedPreferences) and to retrieve such
saved value from the preferences via retrieve(SharedPreferences) followed by getValue().
The key for shared preference must be passed during initialization to one of constructors and can
be later obtained via getKey() or getKeyRes(). Note, that if you will use
resource ids as keys, the raw string key need to be attached via attachKey(Resources),
before any initial storing/obtaining.
| Modifier and Type | Class and Description |
|---|---|
static interface |
SharedPreference.PreferenceChangeCallback<PreferenceType>
Callback that can be used to listen for changes in value of a specific
SharedPreference
implementation. |
| Modifier | Constructor and Description |
|---|---|
protected |
SharedPreference(int keyResId,
Type defaultValue)
Creates a new instance of SharedPreference with the given key resource and default value.
|
protected |
SharedPreference(String key,
Type defValue)
Creates a new instance of SharedPreference with the given key and default value.
|
| Modifier and Type | Method and Description |
|---|---|
SharedPreference<Type> |
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). |
android.content.SharedPreferences.OnSharedPreferenceChangeListener |
createOnChangeListener(SharedPreference.PreferenceChangeCallback<Type> callback)
Creates new instance of
SharedPreferences.OnSharedPreferenceChangeListener which can
be used to listen on changes provided up on the value of this preference within shared preferences. |
Type |
getDefaultValue()
Returns the default value of this preference.
|
String |
getKey()
Returns the key of this preference.
|
int |
getKeyRes()
Return the resource id of the key of this preference.
|
Type |
getValue()
Returns the value, which is currently being hold by this preference object.
|
protected abstract Type |
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 save the actual value into the given shared preferences.
|
SharedPreference<Type> |
retrieve(android.content.SharedPreferences preferences)
Retrieves the current value of this preference from the given shared preferences.
|
boolean |
save(android.content.SharedPreferences preferences)
Saves the current value of this preference into the given shared preferences.
|
SharedPreference<Type> |
updateValue(Type newValue)
Updates the actual value of this preference to the given one.
|
protected SharedPreference(String key, Type 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.protected SharedPreference(int keyResId,
Type defaultValue)
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.public final int getKeyRes()
SharedPreference(int, Object) constructor.public SharedPreference<Type> attachKey(android.content.res.Resources resources)
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 final Type getDefaultValue()
SharedPreference(String, Object).public SharedPreference<Type> retrieve(android.content.SharedPreferences preferences)
preferences - The instance of shared preferences into which was the value of this preference
before saved.getValue()protected abstract Type onGetFromPreferences(android.content.SharedPreferences preferences)
preferences - The instance of shared preferences into which was the value of this preference
before saved.public boolean save(android.content.SharedPreferences 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)protected abstract boolean onPutIntoPreferences(android.content.SharedPreferences preferences)
preferences - The instance of shared preferences into which should be the current value of
this preference saved.True if saving operation succeed, false otherwise.public android.content.SharedPreferences.OnSharedPreferenceChangeListener createOnChangeListener(SharedPreference.PreferenceChangeCallback<Type> callback)
SharedPreferences.OnSharedPreferenceChangeListener which can
be used to listen on changes provided up on the value of this preference within shared preferences.
Note, that here created listener will fire the given callback only in case, that the key received in the callback from shared preferences will match the key of this preference.
callback - Callback to be invoked when the value of this preference changes.SharedPreferencespublic final Type getValue()
retrieve(SharedPreferences)) before this call.
To obtain always an actual value from shared preferences, perform this calls on the instance of SharedPreference of which value you want to obtain:
<b>SharedPreference.parse(SharedPreferences).getActualValue()</b>or<b>SharedPreference.parse(PreferencesManager).getActualValue()</b>
null if this preference's key is
invalid.retrieve(SharedPreferences)public SharedPreference<Type> updateValue(Type newValue)
To immediately save the given new value of this preference into shared preferences, perform this calls on the instance of SharedPreference of which value you want to save:
<b>SharedPreference.updateValue(Type).save(SharedPreferences)</b>or<b>SharedPreference.updateValue(Type).save(PreferencesManager)</b>
newValue - New value for this preference.save(SharedPreferences)