Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the getApplicationContext() method? [duplicate]

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.

like image 896
DLJ Avatar asked May 21 '13 16:05

DLJ


People also ask

When would you call getApplicationContext () and why?

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().

What is 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.

What is the difference between getContext and getApplicationContext?

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).

What is the difference between this and getApplicationContext?

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.


1 Answers

The Context object is used across the application to do many things like access resources that the Android OS has allocated for you.

  • using getSystemService, you get the handle of your service and you can do wonders.
  • pass the context object to your classes to do Toast

It is useful and powerful if used correctly.

like image 176
Siddharth Avatar answered Sep 20 '22 19:09

Siddharth