I've been programming on Android recently, but I'm still not completely clear on the concept of Context
, I'd like you to explain it to me because sometimes you have to go through parameters and other times you don't and I don't understand this, and what's the difference? with this
.
For example in codes like this, why?
TextView mTextView = new TextView(getApplicationContext());
Context
it is an abstract class , and this is implemented by others from there becausecontex
it is not always the same or not always valid, I mean that it cannot always be passed with a getContex() or similar because it is expected from another different implementation, it will depend on the component of the application, for example (the activity, service, Application ect)Application
,Activity/Service
,BroadcastReceiver
,ContentProvider
, ect of which it is being passed, it could be said that it is the access entry to almost the entire Android systemYou can obtain the context by invoking for example
getApplicationContext ()
as mentioned ,getContext ()
,getBaseContext ()
or this from here one of your questions you can only use this when it is in the activity class Activity or that it is somehow related to it, it can be by inheritance ect.When is it used:
It is used for loading resources, getting a
SS
(system service), creating a new activity, getting internal file paths, and for creating new viewsview
that require a context, using Services like LAYOUT_INFLATER_SERVICE, and in other parts that preside over some kind of contextcontext
.ContentProvider : Not really a Context either but one is given when it is created that can be accessed via getContext() . If the ContentProvider is running in the same application process, then this will return the same
singleton
as the application, it will be mentioned in pointApplication
. If the two are in separate processes, thiscontext
will be a newly created package that represents the provider where it is running or running.Activity/Service: This inherits from
ContextWrapper
implementing the same api, known as its context base. When the framework creates a new instance of activity or service a new context is also created, a context will be created for each new instance of an activity or service and in turn its base context for this will be unique for each instance.Application: It is a single instance
patron singleton instance
that is executed in your application process, it can be accessed through methods like getApplication() from an activity or service,Activity
orService
, and usinggetApplicationContext ()
in any other object that inherits from,contex
keep in mind that you will always receive the same instance from within your process.BroadcastReceiver: It is not really a context itself
framework
, it sends, passes or assigns ancontext
enonReceive()
,(PRestrictedContex). every time a new onebroadcast event
arrives is processedbroadcast
and delivered, a new instance of the context is sent.http://developer.android.com/intl/es/reference/android/content/Context.html
Based on SO's answer What is Context in Android
Context
represents the current state of the application and provides information about its execution environment. It is an abstract class and its implementation depends on the Android system.Among other things, it allows access to application-specific resources and classes and also performs operations such as launching activities, generating intents, etc.
Some of the most common uses are:
Create new objects:
TextView mTextView = new TextView(getApplicationContext()); ListAdapter adapter = new SimpleCursorAdapter(getApplicationContext(), ...);
Access common resources like LAYOUT_INFLATER_SERVICE or SharedPreferences
context.getSystemService(LAYOUT_INFLATER_SERVICE)
Implicitly access components:
getApplicationContext().getContentResolver().query(uri, ...);
There are several ways to access the
Context
getApplicationContext()
getContext()
getBaseContext()
this
if you are inside oneActivity