A simple way to manage SharedPreferences

There can be different methods, but this is one that I like the most…

What is SharedPreferences in Android?

In a nutshell, it is like a key-value database, that although the key does not exist, it creates it and sets the value that we have to pass on it, but if the key already exists, it overwrites the value with the new one.

On the other hand, if we need to obtain the value of some key and it does not exist, it will return a default value (this is specified when creating the key).

In this part we will create our class that will take the work of managing the SharedPreferences.

Our UserDefaults class receives a Context as a parameter, since it is necessary for the creation of SharedPreferences.

  • prefName: we can really put the name we want, because this will be the name of our “small database”.
  • prefs: this is our object that makes the reference to SharedPreferences and we use the context that we received in the constructor to be able to create it.

The next step is to add the methods that will make the return of the values we need: Int, String, Boolean, etc… In addition to determining the default value that we will return if the key has not been created.

Methods for Boolean values

private fun setBooleanPrefs(key: String, value: Boolean) {
prefs.edit().putBoolean(key, value).apply()

private fun getBooleanPrefs(key: String): Boolean = prefs.getBoolean(key, false)

Methods for String values

private fun setStringPrefs(key: String, value: String) {
prefs.edit().putString(key, value).apply()

private fun getStringPrefs(key: String): String = prefs.getString(key, "")

Methods for Int values

private fun setIntPrefs(key: String, value: Int) {
prefs.edit().putInt(key, value).apply()

private fun getIntPrefs(key: String): Int = prefs.getInt(key, -1)

Method to clean all saved data

fun clear() {

We have finished the configurations, now we just have to add the variables that we will use as type “key-value tables” as follow

Example with Boolean

var isSessionActive: Boolean
get() = getBooleanPrefs("session_active")
set(value) = setBooleanPrefs("session_active", value)

Example with Int

var userId: Int
get() = getIntPrefs("user_id")
set(value) = setIntPrefs("user_id", value)

Example with String

var accessToken: String
get() = getStringPrefs("accessToken")
set(value) = setStringPrefs("accessToken", value)

The implementation is just as simple and we will see it below.

To begin we will create the instance of UserDefaults passing it a Context

private val defaults: UserDefaults by lazy {

With this we’ll be able to consult and set the values that we need.

System.out.println("User session: ${defaults.isSessionActive}")
defaults.isSessionActive = true
System.out.println("User id: ${defaults.userId}")
defaults.userId = 1
System.out.println("User id: ${defaults.accessToken}")
defaults.accessToken = "1234ABCD"
This is all … you really do not need more, but in this way we have a simple way to access / save the values we need in SharedPreferences