Pages Navigation Menu

"Coding is EC" , "Learn it EC"

Recent-Posts

Stay Connected

Share it now!

Stay Connected

What is Context in Android

Posted by in Android

Context  is context of current state of the application/object.It’s an entity that represents various environment data . Context helps the current activity to interact with out side android environment like  local files, databases, class loaders associated to the environment, services including system-level services, and more
A Context is a handle to the system .I t provides services like resolving resources, obtaining access to databases and preferences, and so on. An android app has activities. It’s like a handle to the environment your application is currently running in. The activity object inherits the Context object.

It lets newly created objects understand what has been going on. Typically you call it to get information regarding another part of your program (activity, package/application).

According to Android Documentation
Interface to global information about an application environment. This is an abstract class whose implementation is provided by the Android system. It allows access to application-specific resources and classes, as well as up-calls for application-level operations such as launching activities, broadcasting and receiving intents, etc.

Different invoking methods by which you can get context

 1. getApplicationContext(),

2.  getContext(),

 3. getBaseContext()

 4. or this (when in the activity class).

Examples for uses of context:

1. Creating New objects: Creating new views, adapters, listeners:


TextView tv = new TextView(getContext());
ListAdapter adapter = new SimpleCursorAdapter(getApplicationContext(), ...);

2. Accessing Standard Common Resources: Services like LAYOUT_INFLATER_SERVICE, SharedPreferences:


context.getSystemService(LAYOUT_INFLATER_SERVICE)  
getApplicationContext().getSharedPreferences(*name*, *mode*);

3. Accessing Components Implicitly: Regarding content providers, broadcasts, intent


getApplicationContext().getContentResolver().query(uri, ...);

You will need the Context class is when creating a view dynamically in an activity. For example, you may want to dynamically create a TextView from code (First Example). To do so, you instantiate the TextView class. The constructor for the TextView class takes a Context object, and because the Activity class is a subclass of Context, you can use the this keyword to represent the Context object.
A Context object provides access to the application’s resources and other features. Each Activity is a Context and each View needs a Context so it can retrieve whatever resources it needs (including things like system-defined resources).

Difference between Activity Context and Application Context:

They are both instances of Context, but the application instance is tied to the lifecycle of the application, while the Activity instance is tied to the lifecycle of an Activity. Thus, they have access to different information about the application environment.

If you read the docs at getApplicationContext it notes that you should only use this if you need a context whose lifecycle is separate from the current context.

But in general, use the activity context unless you have a good reason not to.

Need of Context :

The documentation says that every view needs the context to access the right resources (e.g. the theme, strings etc.).

But why in the constructor and not through setContentView(View)?

1.Because the resources must be accessible while the view is being constructed (the constructor will need some resources to fully initialise the view).

2.This allows the flexibility of using a context that is different from the one of the current activity (imagine a view that uses some other string resources and not the ones from the current activity).

3.The designers of the Android SDK seem to have chosen that the context must be set only once and then stay the same throughout the lifetime of the view.

Why context is not determined automatically at construction point?

1.Because there exists no static variable that would tell you the current global context of your application. The method getApplicationContext() is the closest to this, but it’s not static, so you need an instance of the Activity object to call it.

2.The Java language provides an option to look through the call stack and find whether the View has been constructed in a Context class. But what if there are many? Or what if there are none? This method is very expensive and error prone. So the designers of the API decided that a context must be manually provided.