Android Create Singleton LiveData to Observe Changes (e.g. User Login and Logout) - Kotlin

April 21, 2019

Create Singleton Object

object App {
    class User(val id: String, val name: String, val isAdmin: Boolean)

    var currentUser: User?
        get() = currentUserLiveData.value
        set(value) {

    val currentUserLiveData = MutableLiveData<User?>()

When user sign in/login.

App.currentUser = App.User("5pSFOxspzl75hYZzjnut", "Desmond Lua", true)

When user sign off/logout.

App.currentUser = null

Observer changes in Activity/Fragment.

App.currentUserLiveData.observe(this, Observer { user ->
    // do something
This work is licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License.