Skip to content

Instantly share code, notes, and snippets.

@paraya3636
Last active October 7, 2023 23:07
Show Gist options
  • Save paraya3636/bf8108a75eb49323e56c0c90dd0747e0 to your computer and use it in GitHub Desktop.
Save paraya3636/bf8108a75eb49323e56c0c90dd0747e0 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()
}
}
@NunciosChums
Copy link

thanks

@oguzhangedik
Copy link

thank u

@linsir6
Copy link

linsir6 commented Feb 2, 2018

good !

@WildSaCk
Copy link

WildSaCk commented May 6, 2018

thanks a lot

@FromSi
Copy link

FromSi commented Jun 1, 2018

Perfect

@hesamaldinahani
Copy link

Thanks a lot

@fermisk
Copy link

fermisk commented Sep 7, 2018

thank you!

@mirzaadil
Copy link

Cool, Great Job.

@guacamolie
Copy link

watabout Manifest entry?

@ricardolousada
Copy link

ricardolousada commented May 20, 2019

You are a live saver :)

PS - The Manifest entry should be:

<application
........
.........
android: name = "pathToYourClass.MainApplication">

@nileshkikani
Copy link

how to access it from the activity ?

@ricardolousada
Copy link

how to access it from the activity ?

MainApplication.applicationContext() and you must import the class MainApplication to your activity

@Rwothoromo
Copy link

Goodness gracious! You're the best!

@cepages
Copy link

cepages commented Jul 25, 2019

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

@RishabhDhiman
Copy link

Is this good to follow in mvvm pattern

@sevar83
Copy link

sevar83 commented Sep 8, 2019

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 }
    }
}

@ricardolousada
Copy link

I will test it, seems great @sevar83

@azizejaz786
Copy link

Thanks

@araviind
Copy link

Thank you

@HugoJoly1
Copy link

How am I supposed to implement this into my application ? I put it in a simple kotlin class but I get errors.

@otoo-peacemaker
Copy link

Pls.. how can I use or implement this.

@ATICMCH
Copy link

ATICMCH commented Jan 9, 2023

Thanks!! Love u

@rogergcc
Copy link

rogergcc commented Oct 7, 2023

hi u coulde use in your manifest
In Manifest
<application
android:name="MainApplication " => Will in red then generate autocomplete

In Fragment

MainApplication .applicationContext().resources

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