Install EasyPrefs via Gradle
masterAdd the following dependency to your build.gradle file to include EasyPrefs in your Android project:
dependencies {
implementation 'com.pixplicity.easyprefs:EasyPrefs:1.10.0'
}repository·master·Indexed 19 days ago
https://github.com/pixplicity/easyprefsA lightweight wrapper for Android's SharedPreferences that reduces boilerplate for saving and retrieving primitive data types. It features a Builder for initialization and provides ordered set support via LinkedHashSet to preserve insertion order.
Add the following dependency to your build.gradle file to include EasyPrefs in your Android project:
dependencies {
implementation 'com.pixplicity.easyprefs:EasyPrefs:1.10.0'
}To use EasyPrefs, you must initialize it within the onCreate method of your Application class using the Prefs.Builder. This sets up the global shared preferences instance.
class PrefsApplication : Application() {
override fun onCreate() {
super.onCreate()
// Initialize the Prefs class
Prefs.Builder()
.setContext(this)
.setMode(ContextWrapper.MODE_PRIVATE)
.setPrefsName(packageName)
.setUseDefaultSharedPreference(true)
.build()
}
}Once initialized, you can use static methods to save and retrieve primitive types without manual null checks or contains() calls. If a key does not exist, the retrieval methods return the provided default value.
// Saving values
Prefs.putString("key", "string_value");
Prefs.putLong("key", 123L);
Prefs.putBoolean("key", true);
// Retrieving values
String data = Prefs.getString("key", "default_value");Standard Android getStringSet does not guarantee the order of strings. EasyPrefs provides putOrderedStringSet and getOrderedStringSet which use LinkedHashSet internally to ensure a predictable iteration order and provide compatibility for pre-Honeycomb devices.
// Saving an ordered set
Prefs.putOrderedStringSet("my_set_key", mySet);
// Retrieving an ordered set
Set<String> mySet = Prefs.getOrderedStringSet("my_set_key", new HashSet<String>());