Kotlin singleton application class

You can do the same thing you would do in Java, i.e. put the Application instance in a static field. Kotlin doesn’t have static fields, but properties in objects are statically accessible.

class MyApp: Application() {

    override fun onCreate() {
        super.onCreate()
        instance = this
    }

    companion object {
        lateinit var instance: MyApp
            private set
    }
}

You can then access the property via MyApp.instance.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)