Class DynamicPreferences
It must be initialized once before accessing its methods.
-
Method Summary
Modifier and TypeMethodDescriptionvoid
Remove a key from the default shared preferences.void
Remove a key from the supplied shared preferences.void
Delete the default shared preferences.void
deleteSharedPreferences
(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 DynamicPreferences
Retrieves the singleton instance ofDynamicPreferences
.getSharedPreferences
(String preferences) Returns theSharedPreferences
object for the supplied name.static void
initializeInstance
(Context context) Initialize preferences when application starts.static boolean
Checks whether the key isnull
as listener returnsnull
key when it is removed (or cleared) since API 30.boolean
Retrieve aboolean
preference value from the supplied shared preferences.float
Retrieve afloat
preference value from the supplied shared preferences.int
Retrieve aninteger
preference value from the supplied shared preferences.long
Retrieve along
preference value from the supplied shared preferences.Retrieve aString
preference value from the supplied shared preferences.boolean
Retrieve aboolean
preference value from the supplied shared preferences.float
Retrieve afloat
preference value from the supplied shared preferences.int
Retrieve aninteger
preference value from the supplied shared preferences.long
Retrieve along
preference value from the supplied shared preferences.Retrieve aString
preference 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> void
Save a preference value in the supplied preferences editor and callSharedPreferences.Editor.apply()
to apply changes back from this editor.<T> void
Save a preference value in the supplied preferences editor and callSharedPreferences.Editor.apply()
to apply changes back from this editor.<T> void
Save a preference value in the default shared preferences editor and callSharedPreferences.Editor.apply()
to apply changes back from this editor.<T> void
Save a preference value in the supplied preferences editor and callSharedPreferences.Editor.apply()
to apply changes back from this editor.void
saveStringSet
(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.void
saveStringSet
(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.void
saveStringSet
(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.void
saveStringSet
(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
null
to 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 isnull
as listener returnsnull
key when it is removed (or cleared) since API 30.- Parameters:
key
- The key to be checked.- Returns:
true
if 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
null
to use the default shared preferences.key
- The preference key to be saved or modified.value
- The value for the preference.removeOnNull
-true
to 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
null
to 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
-true
to 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
null
to use the default shared preferences.key
- The preference key to be saved or modified.value
- The value for the preference.removeOnNull
-true
to 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
null
to 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
-true
to 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 aboolean
preference value from the supplied shared preferences.- Parameters:
preferences
- The shared preferences to be used.Set
null
to 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
ClassCastException
if there is a preference with this name that is not aboolean
. - See Also:
-
load
Retrieve aboolean
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
ClassCastException
if there is a preference with this name that is not aboolean
. - See Also:
-
load
Retrieve aninteger
preference value from the supplied shared preferences.- Parameters:
preferences
- The shared preferences to be used.Set
null
to 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
ClassCastException
if there is a preference with this name that is not aninteger
. - See Also:
-
load
Retrieve aninteger
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
ClassCastException
if there is a preference with this name that is not aninteger
. - See Also:
-
load
Retrieve afloat
preference value from the supplied shared preferences.- Parameters:
preferences
- The shared preferences to be used.Set
null
to 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
ClassCastException
if there is a preference with this name that is not afloat
. - See Also:
-
load
Retrieve afloat
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
ClassCastException
if there is a preference with this name that is not afloat
. - See Also:
-
load
Retrieve along
preference value from the supplied shared preferences.- Parameters:
preferences
- The shared preferences to be used.Set
null
to 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
ClassCastException
if there is a preference with this name that is not along
. - See Also:
-
load
Retrieve along
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
ClassCastException
if 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 aString
preference value from the supplied shared preferences.- Parameters:
preferences
- The shared preferences to be used.Set
null
to 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
ClassCastException
if there is a preference with this name that is not aString
. - See Also:
-
load
Retrieve aString
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
ClassCastException
if 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
null
to 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
ClassCastException
if 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
ClassCastException
if there is a preference with this name that is not a Set<String>. - See Also:
-