Make a login session on Android using SharedPreferences

Fijar Lazuardy
Inside PPL B7
Published in
3 min readMay 11, 2020

Do you ever working on a website that requires a login function? You may familiar with JWT, or built-in session function provided by web framework you are using. But how to do that in your Android apps? We know that a web page might have a header that required a token (if you are using JWT), or a login required annotation provided by web framework you are using, but an Android app, specifically a native one, doesn’t have that. So how i achieve it in my Android app? Well luckily, Android provides what is called SharedPreferences

What The Heck Is That?

SharedPreferences is a way to save some data to your internal memory in Android with a key-value concept. It is an interface placed under content package. It is used to store a data, but restricted to only String, Boolean and Integer. And how to implement it to save user’s login info? As well as make them to automatically logged in to the app? Turns out it’s really simple.

Make a SharedPreferencesManager class

It’s actually optional but i found out that it is very much helping me. It makes my code more readable, make it more loosely coupled, also high cohesion. Also making a separated class make it easier to invoke it in every activity needed.

Just as i said, I made a difference class for SharedPreferences Manager (with capitalized class name, of course, read this article to know why). What’s inside that class? Well, it’s pretty straight forward

class SharedPreferencesManager(context: Context) {
private val SP_NAMA = "spName"
private val SP_EMAIL = "spEmail"
private val SP_ISLOGIN = "spIsLogin"
private val sharedPreference: SharedPreferences = context.getSharedPreferences("LoggedIn", Context.MODE_PRIVATE)
private val spEditor = sharedPreference.edit()

fun saveSpString(spKey: String, value: String) {
spEditor.putString(spKey, value)
spEditor.apply()
}

fun saveSpInt(spKey: String, value: Int) {
spEditor.putInt(spKey, value)
spEditor.apply()
}

fun saveSpEmail(spKey: String, value: String) {
spEditor.putString(spKey, value)
spEditor.apply()
}

fun saveSpBoolean(spKey: String, value: Boolean) {
spEditor.putBoolean(spKey, value)
spEditor.apply()
}

fun getName(): String? {
return sharedPreference.getString(SP_NAMA, "")
}

fun getEmail(): String? {
return sharedPreference.getString(SP_EMAIL, "")
}

fun isLogin(): Boolean? {
return sharedPreference.getBoolean(SP_ISLOGIN, false)
}
fun getString(key: String): String? {
return sharedPreference.getBoolean(key, false)
}
fun clearSession() {
spEditor.clear()
spEditor.apply()
}

}

First thing first, we need to make a constructor. This class required a context to be invoked, because sharedPreferences requiring it to get the SharedPreferences from it. To save data to internal storage, we need sharedPreference editor that we created by making a variable from the edit() function out of sharedPreference variable. To save a data, you basically just need to make a setter that takes a key to store it and save it in your internal storage by “put” it using the editor variable and apply it. That’s it, your login info can be simply saved by that, and to get the value, simply make a getter function that gets the value from saved sharedPreferences within the key you saved. For example:

My retrofit2 call for sign in

As you can see, if the login request is success, i save every data necessary to the sharedPreferences variable that is invoked before. So that if the user close the app and re-opening it, the user won’t need to login again that is achieved by adding this line in before your sign in function.

And that’s it, your user won’t be required to login again if they close the app. It’s really as simple and i hope you give it a try if you ever do an Android app project, cheers! :)

--

--