Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding and Using a Context [duplicate]

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.

like image 667
Brigadier Avatar asked Aug 26 '10 06:08

Brigadier


2 Answers

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, ...);
    
like image 190
Sameer Segal Avatar answered Nov 12 '22 23:11

Sameer Segal


Definition of Context

  • Context represents environment data
  • It provides access to things such as databases

Simpler terms (example 1)

  • Consider Person-X is the CEO of a start-up software company.

  • There is a lead architect present in the company, this lead architect does all the work in the company which involves such as database, UI etc.

  • Now the CEO Hires a new Developer.

  • It is the Architect who tells the responsibility of the newly hired person based on the skills of the new person that whether he will work on Database or UI etc.

Simpler terms (example 2)

  • It's like access to android activity to the app's resource.

  • It's similar to when you visit a hotel, you want breakfast, lunch & dinner in the suitable timings, right?

  • There are many other things you like during the time of stay. How do you get these things?

  • You ask the room-service person to bring these things for you.

  • Here the room-service person is the context considering you are the single activity and the hotel to be your app, finally the breakfast, lunch & dinner has to be the resources.


Things that involve context are:

  1. Loading a resource.
  2. Launching a new activity.
  3. Creating views.
  4. obtaining system service.

Context is the base class for Activity, Service, Application, etc

Another way to describe this: Consider context as remote of a TV & channel's in the television are resources, services, using intents, etc - - - Here remote acts as an access to get access to all the different resources into the foreground.

  • So, Remote has access to channels such as resources, services, using intents, etc ....

  • Likewise ... Whoever has access to remote naturally has access to all the things such as resources, services, using intents, etc


Different methods by which you can get context

  • getApplicationContext()
  • getContext()
  • getBaseContext()
  • or this (when in the activity class)

Example:

TextView tv = new TextView(this);

The keyword this refers to the context of the current activity.

like image 572
Devrath Avatar answered Nov 12 '22 22:11

Devrath