Skip to content

Instantly share code, notes, and snippets.

@paraya3636
Last active July 20, 2024 03:28
Show Gist options
  • Select an option

  • Save paraya3636/bf8108a75eb49323e56c0c90dd0747e0 to your computer and use it in GitHub Desktop.

Select an option

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
Copy Markdown

thanks

@oguzhangedik

Copy link
Copy Markdown

thank u

@linsir6

linsir6 commented Feb 2, 2018

Copy link
Copy Markdown

good !

@WildSaCk

WildSaCk commented May 6, 2018

Copy link
Copy Markdown

thanks a lot

@FromSi

FromSi commented Jun 1, 2018

Copy link
Copy Markdown

Perfect

@hesamaldinahani

Copy link
Copy Markdown

Thanks a lot

@fermisk

fermisk commented Sep 7, 2018

Copy link
Copy Markdown

thank you!

@mirzaadil

Copy link
Copy Markdown

Cool, Great Job.

@guacamolie

Copy link
Copy Markdown

watabout Manifest entry?

@ricardolousada

ricardolousada commented May 20, 2019

Copy link
Copy Markdown

You are a live saver :)

PS - The Manifest entry should be:

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

@nileshkikani

Copy link
Copy Markdown

how to access it from the activity ?

@ricardolousada

Copy link
Copy Markdown

how to access it from the activity ?

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

@Rwothoromo

Copy link
Copy Markdown

Goodness gracious! You're the best!

@cepages

cepages commented Jul 25, 2019

Copy link
Copy Markdown

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
Copy Markdown

Is this good to follow in mvvm pattern

@sevar83

sevar83 commented Sep 8, 2019

Copy link
Copy Markdown

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
Copy Markdown

I will test it, seems great @sevar83

@azizejaz786

Copy link
Copy Markdown

Thanks

@araviind

Copy link
Copy Markdown

Thank you

@HugoJoly1

Copy link
Copy Markdown

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
Copy Markdown

Pls.. how can I use or implement this.

@ATICMCH

ATICMCH commented Jan 9, 2023

Copy link
Copy Markdown

Thanks!! Love u

@rogergcc

rogergcc commented Oct 7, 2023

Copy link
Copy Markdown

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