Differences in how to get the context in Android


What are the essential differences between the methods getContext() , getApplicationContext() , getBaseContext() , and this to get the context in Android ?

I know it can be a very trivial or very broad question, but I think that for many of us these concepts are not entirely clear to us and I think it would be of great help to the community. Also, if possible, they can put some basic example of use of each case to improve their understanding.

P.D: with this I mean within a Activity .

asked by Joacer 05.12.2016 в 13:24

3 answers


What are the essential differences between getContext () methods,    getApplicationContext () , getBaseContext () , and this to get the   Context in Android?

getContext () Returns the context in which the view is executed, usually the Activity, through this context you can access the current theme, resources, etc.

getApplicationContext () Returns the context of the single global Application object of the current process. By using this context you ensure that you have a context tied to the life cycle of the application. You could say that it is the most "light" context.

getBaseContext () : The base context set by the constructor or setBaseContext. To talk about this context it is necessary to talk about ContextWrapper which is a class by means of which You can access one context within another. The context referenced by ContextWrapper is obtained through getBaseContext () . Obtaining the context using this method is related to that of the Activity.

This :  This reference can be used in an Activity since Activity inherits from Context and is the reference to the context of the Activity:

   ↳    android.content.Context
       ↳    android.content.ContextWrapper
           ↳    android.view.ContextThemeWrapper
               ↳    android.app.Activity
answered by 05.12.2016 / 17:34

The getApplicationContext() is used to obtain the Context associated with the application, so it will always be the same throughout the life cycle of the app. On the other hand the getBaseContext() is associated with the parent activity, and will not necessarily be the same since it is tied to the life cycle of this, so it can be destroyed.

In summary, you can only have 2 types of Context, the one based on the application and the one based on the activity, so depending on the requirement you must use one or the other.

answered by 05.12.2016 в 15:35

When you do this within an Activity, in reality what you are doing is taking the object Activity of your activity and this Android already takes the context. One way to check this is the following:

We will try to call the getLayoutInflater() method within your activity:

Context context= this;
Activity activity= this;

getLayoutInflater(); //(EstoS dos ultimos casos son iguales)

You will see that with context.getLayoutInflater(); you get an error because this method needs to be called with a Activity object and not with a Context .

answered by 05.12.2016 в 15:03