Forked from paraya3636/KotlinAndroidMainApplication
Created
November 8, 2018 08:24
-
-
Save fida1989/bee1f10d06fa51cee5fac43108d80e27 to your computer and use it in GitHub Desktop.
Kotlin Android MainApplication class for global applicationContext.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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