Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save fida1989/bee1f10d06fa51cee5fac43108d80e27 to your computer and use it in GitHub Desktop.
Save fida1989/bee1f10d06fa51cee5fac43108d80e27 to your computer and use it in GitHub Desktop.
Kotlin Android MainApplication class for global applicationContext.
// 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()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment