Android Context

Mcode App
3 min readSep 26, 2021

What is Context ? and how is it used?

A Context is a handle to the system; It provides services like resolving resources, getting access to databases and preferences, etc. An Android app consists of activities. Context is like a handle to the environment in which your application is currently running. context and why is it so important? To answer this question lets first see what the literal meaning of context is:

Android Context codewith-fun

The circumstances that form the setting for an event, statement, or idea, and in terms of which it can be fully understood

Here are some points of a Context in android: —

  1. Context is the context of the current state of the application.
  2. Context can be for the get information of Activity or an Application, can be used to get access to resources, databases, and shared preferences, and etc.
  3. In Android, Both Activity and Application classes extend the Context class.

Note:- Note:- Wrong use of Context can be lead the memory, data protection leaks in android applications.

The context in Android is really a reference to what we are talking about and where we are currently. This will become more clear as we move forward.

So Contexts are mainly two types: —

  1. ApplicationContext
  2. ActivityContext

ApplicationContext

This context is tied to the life cycle of an application. Mainly this is an instance that is a singleton and can be accessed via getApplicationContext().

. If it is necessary to create a singleton object

. During the necessity of a library in an activity

In Android Using Context of getApplicationContext() some list of functionality as bellow:-

  • Load Resource Values
  • Start a Service
  • Bind to a Service
  • Send a Broadcast
  • Register BroadcastReceiver

Here is example of getApplicationContext().

Inside the activity class, set the name and email of the MyDataModule.class, which can be accessed from another activity. Let us see via the below steps.

Next Step for getBaseContext().

The base context is set by the android class constructor or setBaseContext(). This method is valid only if we have ContextWrapper. Android provides a ContextWrapper class that is built around an existing context:

Like this:-

ContextWrapper wrapper = new ContextWrapper(context);

The most benefit of using a ContextWrapper is that it lets you “modify behavior without changing the original Context

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, …);

https://codewith-fun.medium.com/android-machine-learning-9f1c7f4947ef

https://codewithfun.quora.com/

--

--