Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save vyach-vasiliev/4668906bdce62d99cc5c24c953af6e58 to your computer and use it in GitHub Desktop.
Save vyach-vasiliev/4668906bdce62d99cc5c24c953af6e58 to your computer and use it in GitHub Desktop.
Kotlin Android MainApplication class for global applicationContext.
// Author Keisuke Miura
// Not object class. AndroidManifest.xml error happen.
class MainApplication : Application() {
init {
instance = this
}
companion object {
private var instance: MainApplication? = null
fun applicationContext() : Context {
return instance!!.applicationContext
}
}
override fun onCreate() {
super.onCreate()
// initialize for any
// Use ApplicationContext.
// example: SharedPreferences etc...
val context: Context = MainApplication.applicationContext()
}
}
// author Svetlozar Kostadinov (https://gist.github.com/sevar83)
class MyApp : Application() {
init { INSTANCE = this }
companion object {
lateinit var INSTANCE: MyApp
private set
val applicationContext: Context get() { return INSTANCE.applicationContext }
}
}
@vyach-vasiliev
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment