Skip to content

Instantly share code, notes, and snippets.

@adesamp
Last active March 29, 2021 04:43
Show Gist options
  • Save adesamp/175b3bbec3ed310285c63730f6257dd4 to your computer and use it in GitHub Desktop.
Save adesamp/175b3bbec3ed310285c63730f6257dd4 to your computer and use it in GitHub Desktop.
// init SharedPreferences
companion object {
private const val NAME_KEY = "NAME_KEY"
}
private val preferences = activity.getSharedPreferences("pref_name", Context.MODE_PRIVATE)
private val editor = preferences.edit()
var name: String
// get/load value
get() {
return preferences.getString(NAME_KEY, "").orEmpty()
}
// set/save value
set(value) {
editor.putString(NAME_KEY, value)
editor.commit()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment