Home > DeveloperSection > Interviews > Define different kind of context in android

Posted on    June-11-2015 12:33 AM

 Android Android 
 1 Answer(s)
  903  View(s)
Rate this:

Mayank Tripathi
Mayank Tripathi

Total Post:807

Posted on    June-11-2015 12:33 AM

Context defines the current state of application or object. Context provides access to things such as creating new activity instance, access databases, start a service, etc. You can get the context by invoking getApplicationContext(), getContext(), getBaseContext() or this when in the activity class.

//Creating ui instance
ImageButton button = new ImageButton(getContext());
//creating adapter
ListAdapter adapter = new SimpleCursorAdapter(getApplicationContext(), ...);
//querying content provider
getApplicationContext().getContentResolver().query(uri, ...);
//start activity. Here this means activity context
Intent intent = new Intent(this, SecondActivity.class);

Don't want to miss updates? Please click the below button!

Follow MindStick