Skip to content

Instantly share code, notes, and snippets.

@AkshayChordiya
Created March 22, 2017 06:42
Show Gist options
  • Save AkshayChordiya/6f01bd1ef1fdca8d8a7ff0c2ce7f2b5a to your computer and use it in GitHub Desktop.
Save AkshayChordiya/6f01bd1ef1fdca8d8a7ff0c2ce7f2b5a to your computer and use it in GitHub Desktop.
Kotlin Article: PrefUtils.kt
fun getString(context: Context, key: String, defValue: String): String {
val pref = PreferenceManager.getDefaultSharedPreferences(context)
return pref.getString(key, defValue)
}
fun putString(context: Context, key: String, value: String) {
val pref = PreferenceManager.getDefaultSharedPreferences(context)
val editor = pref.edit()
editor.putString(key, value)
editor.apply()
}
fun getLong(context: Context, key: String, defValue: Long): Long {
val pref = PreferenceManager.getDefaultSharedPreferences(context)
return pref.getLong(key, defValue)
}
fun putLong(context: Context, key: String, value: Long) {
val pref = PreferenceManager.getDefaultSharedPreferences(context)
val editor = pref.edit()
editor.putLong(key, value)
editor.apply()
}
fun exists(context: Context, key: String): Boolean {
val pref = PreferenceManager.getDefaultSharedPreferences(context)
return pref.contains(key)
}
fun remove(context: Context, key: String) {
val pref = PreferenceManager.getDefaultSharedPreferences(context)
val editor = pref.edit()
editor.remove(key)
editor.apply()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment