Android Context

Android Context

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

[HTTP Request in Android with Kotlin
I want to do a login validation using POST method without any third party library and to get some information using GET…t.co](https://t.co/vcslhTWybW "t.co/vcslhTWybW")

[Machine Learning with TensorFlow Lite in mobile apps kotlin/java
Techniques of machine learning in android apps According to machine learning documentation the machine learning is a…codeblogs.info](https://codeblogs.info/machine-learning-with-tensorflow-lite-in-mobile-apps-kotlin-java/ "codeblogs.info/machine-learning-with-tensor..")

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

[Android Core interview questions part 1
What are android applications components ? :-codewith-fun.medium.com](https://codewith-fun.medium.com/android-core-interview-questions-part-1-b5d356056432 "codewith-fun.medium.com/android-core-interv..")

[codewith-fun - Overview
Mobile application developer and Founder of codewith-fun.medium.com - codewith-fungithub.com](https://github.com/codewith-fun "github.com/codewith-fun")

[User codewith-fun
Stack Overflow | The World's Largest Online Community for Developersstackoverflow.com](https://stackoverflow.com/users/12005025/codewith-fun "stackoverflow.com/users/12005025/codewith-fun")

https://codewithfun.quora.com/

Did you find this article valuable?

Support MCODE by becoming a sponsor. Any amount is appreciated!