Sitemap
Châu Anh

Châu Anh

Android Context

--

What is Context in Android ?

Its the context of 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, package/application) You can get the context by invoking getApplicationContext(), getContext(), getBaseContext() or this (when in the activity class).

Few important points about the context:

  • It is the context of the current state of the application.
  • It can be used to get information regarding the activity and application.
  • It can be used to get access to resources, databases, and shared preferences, and etc.
  • Both the Activity and Application classes extend the Context class.

Context is almost everywhere in Android Development and it is the most important thing in Android Development, so we must understand to use it correctly.

Types of Context in Android

There are mainly two types of context are available in Android.

  1. Application Context
  2. Activity Context

The Overall view of the App hierarchy looks like the following

It can be seen in the above image that in “Sample Application”, nearest Context is Application Context. In “Activity1” and “Activity2”, both Activity Context (Here it is Activity1 Context for Activity1 and Activity2 Context for Activity2) and Application Context. The nearest Context to both is their Activity Context only.

Application Context

It is an instance that is the singleton and can be accessed in activity via getApplicationContext(). This context is tied to the lifecycle of an application. The application context can be used where you need a context whose lifecycle is separate from the current context or when you are passing a context beyond the scope of activity. Some use cases of Application Context are:

  • If it is necessary to create a singleton object
  • During the necessity of a library in an activity

getApplicationContext():

It is used to return the context which is linked to the Application which holds all activities running inside it. When we call a method or a constructor, we often have to pass a context and often we use “this” to pass the activity context or “getApplicationContext” to pass the application context. This method is generally used for the application level and can be used to refer to all the activities. For example, if we want to access a variable throughout the android app, one has to use it via getApplicationContext().

Get Châu Anh’s stories in your inbox

Join Medium for free to get updates from this writer.

Activity Context

This context is available in an activity. This context is tied to the lifecycle of an activity. The activity context should be used when you are passing the context in the scope of an activity or you need the context whose lifecycle is attached to the current context. The method of invoking the Activity Context is getContext().

Some use cases of Activity Context are:

  • The user is creating an object whose lifecycle is attached to an activity.
  • Whenever inside an activity for UI related kind of operations like toast, dialogue, etc.,

getContext():

It returns the Context which is linked to the Activity from which it is called. This is useful when we want to call the context from only the current running activity.

Example

This example demonstrates how do I display context in an android textView.

Add the following code to res/layout/activity_main.xml.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/linearLayout"
android:orientation="vertical"
android:padding="16sp"
tools:context=".MainActivity">
</LinearLayout>

Add the following code to src/MainActivity.java

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout linearLayout = findViewById(R.id.linearLayout);
TextView textViewContext = new TextView(getApplicationContext());
textViewContext.setTextSize(30);
textViewContext.setText("Application Context");
textViewContext.setTextColor(Color.RED);
TextView textViewActivityContext = new TextView(this);
textViewActivityContext.setTextSize(30);
textViewActivityContext.setText("Activity Context");
linearLayout.addView(textViewContext);
linearLayout.addView(textViewActivityContext);
}
}

Add the following code to androidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="app.com.example">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

Run this code and see the result

This’s the end of Android Context, If you want to see more, You can get link at reference.

Thank you for reading, have a nice day ❤.

--

--