I am busy learning android and I came across the getApplicationContext
method. I didn't really understand what it was used for and what it's purpose is.
This method is generally used for the application level and can be used to refer to all the activities. For example, if we want to access a variable throughout the android app, one has to use it via getApplicationContext().
getApplicationContext() — returns the Context which is linked to Application which holds all activities running inside it, getBaseContext() —is related to ContextWrapper, which is created around existing Context and let us change its behavior.
getContext() : Returns the context the view is currently running in. Usually the currently active Activity. Activity. getApplicationContext() : Returns the context for the entire application (the process all the Activities are running inside of).
this refers to the current activity ( context ) whereas the getApplicationContext() refers to the Application class. The important differences between the two are that the Application class never has any UI associations and as such has no window token.
The Context object is used across the application to do many things like access resources that the Android OS has allocated for you.
It is useful and powerful if used correctly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With