-
-
Save webserveis/481cc4caf8535ea80c1316b61235248b 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