Sunday, June 10, 2012

What is Context in Android?

In Android programming, what exactly is a Context class and what is it used for? I read about it on the developer site, but I am unable to understand it clearly.

Source: Tips4all


  1. Putting it simply:

    As the name suggests, its the context of current state of the application/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)

    You can get the context by invoking getApplicationContext(), getContext(), getBaseContext() or this (when in the activity class).

    Typical uses of context:

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

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

    Accessing Standard Common Resources:
    Services like LAYOUT_INFLATER_SERVICE, SharedPreferences:

    getApplicationContext().getSharedPreferences(*name*, *mode*);

    Accessing Components Implicitly:
    Regarding content providers, broadcasts, intent

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

  2. A Context is a handle to the system; it 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.

    For more information look here at Section 1.3

  3. An Android Context is an "interface" that allows access to application specific resources and class and information about application environment.

    If your android app was a web app,
    your context would be something
    similar to ServletContext ( I am not making an exact comparison here)

    Your activities and services also extend Context to they inherit all those methods to access the environment information in which the app is running.

  4. context is a reference to current object as this.also context allows access to information about application environment

  5. Context is basically for resource access and getting the environment details of the application(for application context) or activity (for activity context) or any other...

    In order to avoid memory leak you should use application context for every components that needs a context object.... for more click here

  6. ANDROID AND CONTEXT If you look through the various Android APIs, you’ll
    notice that many of them take an android.content.Context object as a
    parameter. You’ll also see that an Activity or a Service is usually used as a
    Context. This works because both of these classes extend from Context.

    What’s Context exactly? Per the Android reference documentation, it’s an
    entity that represents various environment data. It provides access to local
    files, databases, class loaders associated to the environment, services includ-
    ing system-level services, and more. Throughout this book, and in your day-to-
    day coding with Android, you’ll see the Context passed around frequently.
    From: "Android in Practice".

  7. 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.