What Is 'Context' on Android

What is 'Context' on Android?

Putting it simply:

As the name suggests, it's the context of the 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 and package/application).

You can get the context by invoking getApplicationContext(), getContext(), getBaseContext() or this (when in a class that extends from Context, such as the Application, Activity, Service and IntentService classes).

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:

     context.getSystemService(LAYOUT_INFLATER_SERVICE)
    getApplicationContext().getSharedPreferences(*name*, *mode*);
  • Accessing components implicitly:
    Regarding content providers, broadcasts, intent

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

What does the context parameter do in Android and what should it be defined as?

Simple way is

just declare variable as below,

private Context context;

and onCreate() method, assign its value as below,

public void onCreate(Bundle savedInstanceState) 
{
super.onCreate(savedInstanceState);
context = this;
...
}

You can also assign in another way as follows,

context = getApplicationContext();

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

context vs views

This is a strange question. View describes one element of your ui. It can have onClickListeners, properties and so on. But every view is created in some context, usually Activity's context.

Context as itself is something like environment your code is executed in. It has access to ui(if it is an activity), it can contain some global data(application context), and has access to the resources(all of the contexts). Also, context allows you to perform common android operations like broadcasting intents, start activities and services.

So, views should be passed when you want to do something with a particular view.
Context is passed when you need access to resources, global data or ui context, or launch other android components.

How many types of context in android and what is better to use

For Your better understands you should read android official blog. an also look at HackBod Answer.

There are some references URL which help you more about the context

  1. What exactly does using the Application Context mean?
  2. Difference between Activity Context and Application Context
  3. http://android-developers.blogspot.de/2009/01/avoiding-memory-leaks.html

Thanks

How do I learn which Context I have to use for each situation in Android?

There are mainly two type of context in android & both of them are provided to you as a ContextWrapper class object.

  1. Application Context

This context is basically provided/used when you need to do some work that doesn't involves any view/UI operation (where you don't need to refer to/rely on any UI code).

This is mostly helpful when you're doing operations directly via your process that doesn't need any UI interactions like for an example, getting list of contacts via content provider.

So, you can consider this as parent of all other context you can have in your application and apparently this is consistent for your given process.

This context is retrieved via Application class of your app, or via any other activity by requesting like (activity.applicationContext).


  1. Activity Context

This context is basically bounded/limited to given activity you're in right now. This is helpful to mainly do UI operations because, your context is part of the same UI you are in right now.

So, yes you can consider this a child context that can changes as per your activity changes. For most of the cases, it is not preferred to store & share this context across UI/Activities as it can lead to memory leaks.

This context can be retrieved once you're on any activity class via (Activity.this, this, context in fragments & custom views)


I agree personally that context has been confusing topic on Android since the beginning, but if you know C/C++ then I can relate this to 'pointers' (conceptually). Context are pointers for given activity/application class.

MainActivity context Android

Retrieve the Intent in onCreate, pull everything you need out of it and then you can store it in a number of places.

SharedPreferences or a Database or Static Variables or a Singleton

When you finally need the Intent data you can pull it from a place that is more permanent, instead of an Activity which is not.



Related Topics



Leave a reply



Submit