Siddroid - your place to learn Kotlin and Android

Siddroid

Browse

Search

About

Understanding Context in Android

  ·  2 min read

  ·  790 views

Understanding context in Android and the implementation

If you have recently started to develop apps on Android or have been developing them from a long time, you know what context is. The same old way to access everything that the application provides, it’s resources and application’s environment. In this article we will deep dive into what context is and attempt to understand it better.


What is context?

If you look into the context class on Android Studio by hitting cmd + b you’ll see some methods like these

    public abstract AssetManager getAssets();

    public abstract Resources getResources();

    public abstract PackageManager getPackageManager();

    public abstract ContentResolver getContentResolver();

    public abstract Looper getMainLooper();

most of these methods look familiar and I’m sure you have used these. But what is context?

Context is an abstract class and a part of Android’s content package. Context allows us to access application’s resources, classes and for performing various operations such as launching an activity or firing intents. The implementation of this abstract class is provided by Android’s system.

Simple right? It actually is. But if I ask you a simple question for example like what is the difference between activity context and application context, will you be able to differentiate between the two?

Context implementation


What is context in Android? How is context implemented? Understanding context wrapper and context theme wrapper in Android

Let’s look at each component in the above diagram in detail.

  • Context: An abstract class, part of android’s content package.

  • Android System: The OS itself

  • Context Implementation: The implementation of the abstract class on Android’s system.

  • Context Wrapper: The wrapper class that encapsulates the functionalities of the implementation

  • ContextThemeWrapper: Another wrapper which allows you to modify or replace the theme of the context.

  • Activity, Application & Service: What we developers get to utilize and access the resources and environment.

Notice how application and service don’t need a theme wrapper. Only activity does as that is where our UI magic happens.

Things you can and can’t do with context:

  • You can start an activity from another activity. You can’t start an activity using Application context. (Since application context is unaware of the activity backstack i.e the sequence of activities)

  • You can use context to get resources, strings and layouts using resource identifiers.

  • You can get application context using getApplication() or getApplicationContext(). In broadcast receivers you can only use the later.

Proceed with caution!

  • Don’t store activity context in view models.

Next time you make an app, you’ll have a context to what context is!

About the author

Siddhesh Patil is an Android Engineer at DeliveryHero. Building core ordering experiences at Foodpanda and Foodora. He is the organzier of Kotlin Mumbai, a developer community in Mumbai, India. Find talks, articles and latest developments in Android and Kotlin on this website siddroid.com

Sid Patil is an Android Engineer and Kotlin Advocate based in Berlin, Germany. He works at Delivery Hero, building ordering experiences of food delivery and Q-commerce brands like Foodpanda and Foodora.

This publication was last updated on November 23, 2021

Share on