What is the difference between getContext()
, getApplicationContext()
, getBaseContext()
, and "this
"?
Though this is simple question I am unable to understand the basic difference between them. Please give some easy examples if possible.
What is the difference between getContext()
, getApplicationContext()
, getBaseContext()
, and "this
"?
Though this is simple question I am unable to understand the basic difference between them. Please give some easy examples if possible.
The question "what the Context is" is one of the most difficult questions in Android universe.
Context defines methods that access system resources, retrieve application's static assets, check permissions, perform UI manipulations and many more. In essence,
Context
is an example of God Object anti-pattern in production.When it comes to which kind of
Context
should we use, it becomes very complicated because except for being God Object, the hierarchy tree ofContext
subclasses violates Liskov Substitution Principle brutally.This blog post attempts to summarize
Context
classes applicability in different situations.Let me copy the main table from that post for completeness:
View.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). Use this instead of the current Activity context if you need a context tied to the lifecycle of the entire application, not just the current Activity.ContextWrapper.getBaseContext()
: If you need access to a Context from within another context, you use a ContextWrapper. The Context referred to from inside that ContextWrapper is accessed via getBaseContext().From this docs
I understood that you should use:
Most answers already cover
getContext()
andgetApplicationContext()
but getBaseContext() is rarely explained.The method
getBaseContext()
is only relevant when you have aContextWrapper
. Android provides aContextWrapper
class that is created around an existingContext
using:The benefit of using a
ContextWrapper
is that it lets you “modify behavior without changing the original Context”. For example, if you have an activity calledmyActivity
then can create aView
with a different theme thanmyActivity
:ContextWrapper
is really powerful because it lets you override most functions provided byContext
including code to access resources (e.g.openFileInput()
,getString()
), interact with other components (e.g.sendBroadcast()
,registerReceiver()
), requests permissions (e.g.checkCallingOrSelfPermission()
) and resolving file system locations (e.g.getFilesDir()
).ContextWrapper
is really useful to work around device/version specific problems or to apply one-off customizations to components such as Views that require a context.The method getBaseContext() can be used to access the “base” Context that the
ContextWrapper
wraps around. You might need to access the “base” context if you need to, for example, check whether it’s aService
,Activity
orApplication
:Or if you need to call the “unwrapped” version of a method:
getApplicationContext()
getContext() and getBaseContext()
this