A class that saves time of writing boilerplate code for saving any kind of data in shared preferences
Step 1. Add the JitPack repository to your build file
Add it in your root build.gradle at the end of repositories:
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
Step 2. Add the dependency
dependencies {
implementation 'com.github.islamdidarmd:Kpref:0.1.1'
}
//save values in a default sharedPreference
Kprefs.initDefault(this)
Kprefs.save("test", 10)
val x: Int? = Kprefs.get("")
//save values in your preference
val pref = getSharedPreferences("my_preference", Context.MODE_PRIVATE)
Kprefs.init(pref)
Kprefs.save("testMyPref", "Saved")
val value: String? = Kprefs.get("")
You must call Kprefs.initDefault(this)
or Kprefs.init(pref)
before calling any methods
For a complete example, see the sample app