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:
java.lang.Object
↳ android.content.Context
↳ android.content.ContextWrapper
↳ android.view.ContextThemeWrapper
↳ android.app.Activity