24
What is a context
on Android?
What’s the difference between getContext()
, getApplicationContext()
, getBaseContext()
?
Has some relationship with getActivity?
24
What is a context
on Android?
What’s the difference between getContext()
, getApplicationContext()
, getBaseContext()
?
Has some relationship with getActivity?
43
(References: official documentation and two questions soen.)
It is an access point for global information about a application environment. This is an abstract class whose implementation is provided by Android system. It allows access to features and application-specific classes, as well as calls to Application-level operations how to start activities, send or receive intents for broadcast, etc..
In other words, Context
is the way provided by the system for your application to access certain features (such as the start feature of a Activity, start or stop a service, send a broadcast, open a database or preferences file, etc.) or classes (like the various Managers
the system offers: Telephony, Alarm, Sensors, Audio, Notifications, Power, USB, etc.). These features and classes have the particularity of being global at the application level, that is, they are application-level. It is still a God Object as says the Square - and, indeed, without an instance of Context
you don’t do much in an Android app.
Base class for those who need to preserve global application status. You can provide your own implementation (usually there is no need, and static singletons can provide the same functionality in a more modular way).
An Activity is a unique and focused thing that the user can do.
It is an application component that can either represent an application’s desire to run a longer operation while avoiding interacting with the user or providing functionality for other applications to use.
Application
, Activity
and Service
are achievements of Context
, that is, concrete classes that implement a Context
Android. When you extend these classes into your code, you have access to the application-level services provided by Context
inherited by these classes. In addition, you have access to resources specific to each subclass; for example, to Activity
active at the moment can be closed through the method finish()
and can execute code on the main thread in a simple way through the method runOnUiThread()
. Activities
, Services
and Applications
have each their particular life cycle. And not always the contexts are interchangeable; for example, if you try to display a Dialog
passing to him a context Application
, this will cause an error due to Android waiting for it to be passed a Activity
(this particular problem is more of an Android idiosyncrasy, which should expect to receive soon a Activity
).
View.getContext()
: Returns the context in which the view is being displayed. Generally the Activity
active at the moment.
Activity.getApplicationContext()
: Returns the context of the entire application (the process within which all Activities are being performed). Use this in place of the current active Activity context if you are in need of a context linked to the entire application lifecycle.
ContextWrapper.getBaseContext()
: It’s a class method ContextWrapper
. And ContextWrapper
is "an implementation proxy of Context
that simply delegates all your calls to another Context
. Can be extended to modify behaviors without changing the Context
original."
Fragment.getActivity()
: Returns to Activity
to which this fragment is attached. A fragment does not have Context
by itself; but when it is connected (Attached or Added) to a Activity
has access to the context of Activity
, or an instance of Application
has access to that instance even if it is disconnected from Activity
.
Great answer.
3
As the name suggests, it is the context of the object or application. It is a way to access in the code the current state of the application.
On Android, Context
is the base class for Activity
, Service
, Application
, therefore, it is a way to access and deal with your application via code.
Browser other questions tagged android android-context
You are not signed in. Login or sign up in order to post.
According to the Square the
Context
is a God Object, andActivity
is aContext
that comes with a life cycle. ;)– Piovezan
Can you give a more precise answer? For me to accept
– felipe.rce