Dynamic Preferences
Manage shared preferences on Android.
- Pranav Pandey
- Android | Library
- 22 Nov, 2019
- 1 Min read
A library to manage shared preferences on Android 4.0 (API 14) and above devices.
ToC
Table of Contents
Background
I have various apps on Google Play and sometimes it is required to access shared preferences from anywhere within an app. So, I wrote this library to provide that functionality easily and quickly. Please read below to know more about its usage.
Usage
It is based on the singleton
approach to manage shared preferences within an app. This library is fully commented so I am highlighting some of the features below.
Please follow and keep exploring the GitHub repository to find out more hidden features.
Initialize
DynamicPreferences
must be initialized once before accessing its methods.
// Initialize with application context.
DynamicPreferences.initializeInstance(applicationContext);
After initializing, its various public methods can be accessed via getting the initialized instance.
Save
It supports the saving of boolean
, int
, String
and Set<String>
value types into the shared preferences.
// Save a value in the default shared preferences.
DynamicPreferences.getInstance().save(key, value);
// Save a value in the supplied shared preferences.
DynamicPreferences.getInstance().save(preferences, key, value);
Load
It supports the retrieval of boolean
, int
, String
and Set<String>
value types from the shared preferences.
// Retrieve a value from the default shared preferences.
DynamicPreferences.getInstance().load(key, defaultValue);
// Retrieve a value from the supplied shared preferences.
DynamicPreferences.getInstance().save(preferences, key, defaultValue);
Delete
It supports the deletion of a particular key
or a complete shared preferences.
// Remove a key from the default shared preferences.
DynamicPreferences.getInstance().delete(key);
// Remove a key from the supplied shared preferences.
DynamicPreferences.getInstance().delete(preferences, key);
// Delete a shared preferences.
DynamicPreferences.getInstance().deleteSharedPreference(preferences);
Dependency
It depends on the dynamic-utils to perform various internal operations. So, its functions can also be used to perform other useful operations.
Installation
Please follow the GitHub repository for the installation guide. I hope it will help you in your projects and please ask questions or share your feedback in the comments section to make it better.