SharedPreference in android
class Preference(appContext: Context) {
private val preferenceName = "preference"
private val sharedPref: SharedPreferences =
appContext.getSharedPreferences(preferenceName, Context.MODE_PRIVATE)
private val editor: SharedPreferences.Editor = sharedPref.edit()
fun save(KEY_NAME: String, value: Int) {
editor.apply {
putInt(KEY_NAME, value)
commit()
}
}
fun save(KEY_NAME: String, value: String) {
editor.apply {
putString(KEY_NAME, value)
commit()
}
}
fun save(KEY_NAME: String, value: Boolean) {
editor.apply {
putBoolean(KEY_NAME, value)
commit()
}
}
fun getString(KEY_NAME: String): String? {
return sharedPref.getString(KEY_NAME, AppConstants.Default.STRING)
}
fun getBoolean(KEY_NAME: String): Boolean? {
return sharedPref.getBoolean(KEY_NAME, AppConstants.Default.BOOLEAN)
}
fun getInt(KEY_NAME: String): Int? {
return sharedPref.getInt(KEY_NAME, AppConstants.Default.INT)
}
}