What is Context in Android

Context in Android is an 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 ways of getting context

→ getContext()
→ getBaseContext()
→ getApplicationContext()
→ this

Usage of context

→ Loading common resources
→ Creating dynamic views
→ Displaying Toast messages
→ Launching Activities etc.

Examples

→ TextView tv = new TextView(getContext());
→ ListAdapter adapter = new SimpleCursorAdapter(getApplicationContext(), …);
→ Toast toast = Toast.makeText(ActivityName.this, “Context Example”, Toast.LENGTH_LONG).show();
→ getApplicationContext().getContentResolver().query(uri, …);

Enjoy Coding and Share Knowledge




Show Buttons
Hide Buttons