Class DynamicPreferences
It must be initialized once before accessing its methods.
-
Method Summary
Modifier and TypeMethodDescriptionvoidRemove a key from the default shared preferences.voidRemove a key from the supplied shared preferences.voidDelete the default shared preferences.voiddeleteSharedPreferences(String preferences) Delete a shared preferences.Returns the context used by this instance.Get the default shared preferences name to perform other operations like backup and restore.static DynamicPreferencesRetrieves the singleton instance ofDynamicPreferences.getSharedPreferences(String preferences) Returns theSharedPreferencesobject for the supplied name.static voidinitializeInstance(Context context) Initialize preferences when application starts.static booleanChecks whether the key isnullas listener returnsnullkey when it is removed (or cleared) since API 30.booleanRetrieve abooleanpreference value from the supplied shared preferences.floatRetrieve afloatpreference value from the supplied shared preferences.intRetrieve anintegerpreference value from the supplied shared preferences.longRetrieve alongpreference value from the supplied shared preferences.Retrieve aStringpreference value from the supplied shared preferences.booleanRetrieve abooleanpreference value from the supplied shared preferences.floatRetrieve afloatpreference value from the supplied shared preferences.intRetrieve anintegerpreference value from the supplied shared preferences.longRetrieve alongpreference value from the supplied shared preferences.Retrieve aStringpreference value from the supplied shared preferences.loadStringSet(String preferences, String key, Set<String> value) Retrieve a Set<String> preference value from the supplied shared preferences.loadStringSet(String key, Set<String> value) Retrieve a Set<String> preference value from the supplied shared preferences.<T> voidSave a preference value in the supplied preferences editor and callSharedPreferences.Editor.apply()to apply changes back from this editor.<T> voidSave a preference value in the supplied preferences editor and callSharedPreferences.Editor.apply()to apply changes back from this editor.<T> voidSave a preference value in the default shared preferences editor and callSharedPreferences.Editor.apply()to apply changes back from this editor.<T> voidSave a preference value in the supplied preferences editor and callSharedPreferences.Editor.apply()to apply changes back from this editor.voidsaveStringSet(String preferences, String key, Set<String> value) Save a Set<String> in the supplied preferences editor and callSharedPreferences.Editor.apply()to apply changes back from this editor.voidsaveStringSet(String preferences, String key, Set<String> value, boolean removeOnNull) Save a Set<String> in the supplied preferences editor and callSharedPreferences.Editor.apply()to apply changes back from this editor.voidsaveStringSet(String key, Set<String> value) Save a Set<String> in the default preferences editor and callSharedPreferences.Editor.apply()to apply changes back from this editor.voidsaveStringSet(String key, Set<String> value, boolean removeOnNull) Save a Set<String> in the default preferences editor and callSharedPreferences.Editor.apply()to apply changes back from this editor.
-
Method Details
-
initializeInstance
Initialize preferences when application starts.Must be initialized once.
- Parameters:
context- The context to retrieve resources.
-
getInstance
Retrieves the singleton instance ofDynamicPreferences.Must be called before accessing the public methods.
- Returns:
- The singleton instance of
DynamicPreferences.
-
getContext
Returns the context used by this instance.- Returns:
- The context to retrieve the resources.
-
delete
Remove a key from the supplied shared preferences.- Parameters:
preferences- The shared preferences to be used.Set
nullto use the default shared preferences.key- The preference key to remove.- See Also:
-
delete
Remove a key from the default shared preferences.- Parameters:
key- The preference key to remove.- See Also:
-
isNullKey
Checks whether the key isnullas listener returnsnullkey when it is removed (or cleared) since API 30.- Parameters:
key- The key to be checked.- Returns:
trueif the key isnull.
-
save
public <T> void save(@Nullable String preferences, @Nullable String key, @Nullable T value, boolean removeOnNull) Save a preference value in the supplied preferences editor and callSharedPreferences.Editor.apply()to apply changes back from this editor.- Type Parameters:
T- The type of shared preference.- Parameters:
preferences- The shared preferences to be used.Set
nullto use the default shared preferences.key- The preference key to be saved or modified.value- The value for the preference.removeOnNull-trueto remove key if the value isnull.- See Also:
-
save
Save a preference value in the supplied preferences editor and callSharedPreferences.Editor.apply()to apply changes back from this editor.- Type Parameters:
T- The type of shared preference.- Parameters:
preferences- The shared preferences to be used.Set
nullto use the default shared preferences.key- The preference key to be saved or modified.value- The value for the preference.- See Also:
-
save
Save a preference value in the supplied preferences editor and callSharedPreferences.Editor.apply()to apply changes back from this editor.- Type Parameters:
T- The type of shared preference.- Parameters:
key- The preference key to be saved or modified.value- The value for the preference.removeOnNull-trueto remove key if the value isnull.- See Also:
-
save
Save a preference value in the default shared preferences editor and callSharedPreferences.Editor.apply()to apply changes back from this editor.- Type Parameters:
T- The type of the shared preference.- Parameters:
key- The preference key to be saved or modified.value- The value for the preference.- See Also:
-
saveStringSet
public void saveStringSet(@Nullable String preferences, @Nullable String key, @Nullable Set<String> value, boolean removeOnNull) Save a Set<String> in the supplied preferences editor and callSharedPreferences.Editor.apply()to apply changes back from this editor.- Parameters:
preferences- The shared preferences to be used.Set
nullto use the default shared preferences.key- The preference key to be saved or modified.value- The value for the preference.removeOnNull-trueto remove key if the value isnull.- See Also:
-
saveStringSet
public void saveStringSet(@Nullable String preferences, @Nullable String key, @Nullable Set<String> value) Save a Set<String> in the supplied preferences editor and callSharedPreferences.Editor.apply()to apply changes back from this editor.- Parameters:
preferences- The shared preferences to be used.Set
nullto use the default shared preferences.key- The preference key to be saved or modified.value- The value for the preference.- See Also:
-
saveStringSet
Save a Set<String> in the default preferences editor and callSharedPreferences.Editor.apply()to apply changes back from this editor.- Parameters:
key- The preference key to be saved or modified.value- The value for the preference.removeOnNull-trueto remove key if the value isnull.- See Also:
-
saveStringSet
Save a Set<String> in the default preferences editor and callSharedPreferences.Editor.apply()to apply changes back from this editor.- Parameters:
key- The preference key to be saved or modified.value- The value for the preference.- See Also:
-
load
Retrieve abooleanpreference value from the supplied shared preferences.- Parameters:
preferences- The shared preferences to be used.Set
nullto use the default shared preferences.key- The preference key to be retrieved.value- The default value for the preference.- Returns:
- Returns the preference value if it exists, or the default value.
Throws
ClassCastExceptionif there is a preference with this name that is not aboolean. - See Also:
-
load
Retrieve abooleanpreference value from the supplied shared preferences.- Parameters:
key- The preference key to be retrieved.value- The default value for the preference.- Returns:
- Returns the preference value if it exists, or the default value.
Throws
ClassCastExceptionif there is a preference with this name that is not aboolean. - See Also:
-
load
Retrieve anintegerpreference value from the supplied shared preferences.- Parameters:
preferences- The shared preferences to be used.Set
nullto use the default shared preferences.key- The preference key to be retrieved.value- The default value for the preference.- Returns:
- Returns the preference value if it exists, or the default value.
Throws
ClassCastExceptionif there is a preference with this name that is not aninteger. - See Also:
-
load
Retrieve anintegerpreference value from the supplied shared preferences.- Parameters:
key- The preference key to be retrieved.value- The default value for the preference.- Returns:
- Returns the preference value if it exists, or the default value.
Throws
ClassCastExceptionif there is a preference with this name that is not aninteger. - See Also:
-
load
Retrieve afloatpreference value from the supplied shared preferences.- Parameters:
preferences- The shared preferences to be used.Set
nullto use the default shared preferences.key- The preference key to be retrieved.value- The default value for the preference.- Returns:
- Returns the preference value if it exists, or the default value.
Throws
ClassCastExceptionif there is a preference with this name that is not afloat. - See Also:
-
load
Retrieve afloatpreference value from the supplied shared preferences.- Parameters:
key- The preference key to be retrieved.value- The default value for the preference.- Returns:
- Returns the preference value if it exists, or the default value.
Throws
ClassCastExceptionif there is a preference with this name that is not afloat. - See Also:
-
load
Retrieve alongpreference value from the supplied shared preferences.- Parameters:
preferences- The shared preferences to be used.Set
nullto use the default shared preferences.key- The preference key to be retrieved.value- The default value for the preference.- Returns:
- Returns the preference value if it exists, or the default value.
Throws
ClassCastExceptionif there is a preference with this name that is not along. - See Also:
-
load
Retrieve alongpreference value from the supplied shared preferences.- Parameters:
key- The preference key to be retrieved.value- The default value for the preference.- Returns:
- Returns the preference value if it exists, or the default value.
Throws
ClassCastExceptionif there is a preference with this name that is not along. - See Also:
-
load
@Nullable public String load(@Nullable String preferences, @Nullable String key, @Nullable String value) Retrieve aStringpreference value from the supplied shared preferences.- Parameters:
preferences- The shared preferences to be used.Set
nullto use the default shared preferences.key- The preference key to be retrieved.value- The default value for the preference.- Returns:
- Returns the preference value if it exists, or the default value.
Throws
ClassCastExceptionif there is a preference with this name that is not aString. - See Also:
-
load
Retrieve aStringpreference value from the supplied shared preferences.- Parameters:
key- The preference key to be retrieved.value- The default value for the preference.- Returns:
- Returns the preference value if it exists, or the default value.
Throws
ClassCastExceptionif there is a preference with this name that is not aString. - See Also:
-
loadStringSet
@Nullable public Set<String> loadStringSet(@Nullable String preferences, @Nullable String key, @Nullable Set<String> value) Retrieve a Set<String> preference value from the supplied shared preferences.- Parameters:
preferences- The shared preferences to be used.Set
nullto use the default shared preferences.key- The preference key to be retrieved.value- The default value for the preference.- Returns:
- Returns the preference value if it exists, or the default value.
Throws
ClassCastExceptionif there is a preference with this name that is not a Set<String>. - See Also:
-
loadStringSet
Retrieve a Set<String> preference value from the supplied shared preferences.- Parameters:
key- The preference key to be retrieved.value- The default value for the preference.- Returns:
- Returns the preference value if it exists, or the default value.
Throws
ClassCastExceptionif there is a preference with this name that is not a Set<String>. - See Also:
-