1

I'm used to writing iOS apps in Swift, and using UserDefaults to safe state. How do I do something similar in Android using Kotlin?

For example, here's how I would set the user's data and login state in Swift.

// Swift example; how is this possible with Kotlin?
UserDefaults.standard.set(value, forKey: MyKeys.userid.rawValue)
UserDefaults.standard.synchronize()
Ky -
  • 30,724
  • 51
  • 192
  • 308
Acetech
  • 398
  • 4
  • 16

2 Answers2

5

The equivalent of UserDefaults in Android is SharedPreferences

To use SharedPreferences first you can get an object of SharedPreferences this way:

val sharedPreferences = getSharedPreferences(name:String!, mode: Int)

in case you are in an activity you can call the method directly since activity inherits from Context otherwise you need a context object to call getSharedPreferences. The name can be anything you'd like to call your preferences and for the mode you can find more here.

Next to save data in the sharedPreferences you call the method sharedPreferences.edit() which returns an Editor object, you put data in the editor and to save the changes you call commit() for example:

sharedPreferences.edit().putInt(key: String!, value: Int).commit()

as you can see we've put an integer to the editor and then we called commit().

To fetch the data we save is pretty simple we just call the corresponding get method to the type for example

sharedPreferences.getInt(key: String!, defaultValue: Int)
Alban Gashi
  • 603
  • 4
  • 14
4
const val SHARED_PREF = "sharedPreferences"
val sharedPreferences = getSharedPreferences(SHARED_PREF, Context.MODE_PRIVATE)
         <----- Start of Setting the value ----->
        val userLoggedIn = sharedPreferences.edit()
        // setting it as a boolean
        userLoggedIn.putBoolean("isloggedin", true)

        // setting it as a string
       userLoggedIn.putString("isloggedin", "1")

       // setting it as an Int
       userLoggedIn.putInt("isloggedin", 1)

       // committing or applying is the last step to save, without this those data above wont be saved. commit returns a boolean, apply does not
        userLoggedIn.commit() or userLoggedIn.apply()

            <----- End Of Setting the value -----/>
---------------------------------------------------------------------------
             <----- Start of Retrieving the value ----->
       // Retrieving it as String
       //"nil" below is a default value, just incase "isloggedin" as a string does not exist, it returns "nil"
       val retrieveIsLoggedin =  sharedPreferences.getString("isloggedin", "nil")

       // Retrieving it as an Int
      //0 below is a default value, just incase "isloggedin" as an int does not exist, it returns 0
       val retrieveIsLoggedin =  sharedPreferences.getInt("isloggedin", 0)

       // Retrieving it as an Boolean
      //false below is a default value, just incase "isloggedin" as a boolean does not exist, it returns false
       val retrieveIsLoggedin=  sharedPreferences.getBoolean("isloggedin", false)


             <----- End of Retrieving the value ----->
Acetech
  • 398
  • 4
  • 16