-
-
Save paraya3636/bf8108a75eb49323e56c0c90dd0747e0 to your computer and use it in GitHub Desktop.
// 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() | |
} | |
} |
thank u
good !
thanks a lot
Perfect
Thanks a lot
thank you!
Cool, Great Job.
watabout Manifest entry?
You are a live saver :)
PS - The Manifest entry should be:
<application
........
.........
android: name = "pathToYourClass.MainApplication">
how to access it from the activity ?
how to access it from the activity ?
MainApplication.applicationContext() and you must import the class MainApplication to your activity
Goodness gracious! You're the best!
You are a live saver :)
PS - The Manifest entry should be:
<application
........
.........
android: name = "pathToYourClass.MainApplication">
Do I have to do something extra to make it work in my tests? It seems in the unit test, this part fails
fun applicationContext() : Context {
return instance!!.applicationContext
}
In particular, instance
is null
Is this good to follow in mvvm pattern
What about this (a bit) simpler variant?
class MyApp : Application() {
init { INSTANCE = this }
companion object {
lateinit var INSTANCE: MyApp
private set
val applicationContext: Context get() { return INSTANCE.applicationContext }
}
}
I will test it, seems great @sevar83
Thanks
Thank you
How am I supposed to implement this into my application ? I put it in a simple kotlin class but I get errors.
Pls.. how can I use or implement this.
Thanks!! Love u
hi u coulde use in your manifest
In Manifest
<application
android:name="MainApplication " => Will in red then generate autocomplete
In Fragment
MainApplication .applicationContext().resources
thanks