The Android Context, Manifest, and the Android System

The Application & the Android System

  1. The applications need a way to communicate with the Android System, so they can request it to construct and provide any components and resources that are otherwise inaccessible directly by code.
  2. The Android System should keep track of all installed applications and their components, in order to launch or provide them back to the caller when receiving a request that fits their existence.

Application Manifest

  1. Package Information of the application, for the OS to add the app to the Application Layer of the Android Software Stack and assign a unique user to run it, so the app can become reachable after installation
  2. Permissions using the <permission> tag for Android to know what restricted hardware and software resources should be enabled during app execution
  3. and any custom subclass implementation of the Four Application Components for Android to be able to provide them upon request. These are
    <activity> for Activities,
    <service> for Services,
    <provider> for Content Providers, and
    <receiver> for Broadcast Receivers.

Let’s start with intents

Application Components in the Manifest

  • Activities & Manifest: Android gets aware of all the activities so it can create and launch them explicitly or implicitly upon intent requests.
  • Services & Manifest: Android gets aware of all the available services so it knows what background operations it can run.
  • Content Providers & Manifest: Android is aware of all the content providers, so it can provide content to your app or other apps when needed.
  • Broadcast Receivers & Manifest: Android is aware of all receivers registered to listen to a broadcast (via manifest declaration), so even when the app is not running, the receivers that are registered to a specific broadcast will be notified.

What is Android Manifest — TL;DR

Context & Non-Context Manifest Components

Blue: Context Components — Green: Non-Context Components

Context Manifest Components

  • Application
  • Activity
  • Service

Non-Context manifest components

  • Content Providers
  • Broadcast Receivers

Android Context

  • Activity inherits from ContextThemeWrapper, while Service and Application do not (see that “Theme” word in that class name?). As ApplicationContext is not UI related, it cannot be used to inflate layouts, start activities, or display dialogues. If you attempt to affect the UI using a Context that is not UI related, you will only crash your application.
  • So, depending on what context we pass, we get access to different information about the application’s environment, like with two different UI Context objects at the same application that use a different theme (take a look at this awesome video from Coding in Flow — also look at this article about ContextThemeWrapper).
  • Now for other tasks, like for a non-UI singleton object that will be used throughout the application, and requires context (like access to a database) you will use ApplicationContext to avoid memory leaks because you will hold the same database reference throughout the application lifetime.

The roles of Context in method calls

  • Passive role: To ask the OS for some value (eg: what is the value of a resource).
  • Active role: To ask the OS to perform some action on your behalf (eg: launching a component through an Intent, inflate some layout, display a message, etc).
  • Binding role: To connect your app to a distant entity or mechanism managed by the OS (eg: connecting to an SQLite database, where that binding will be used in both “active” and “passive” roles to read or update data in the database — in such cases, you almost always use an Application Context, as the binding should exist throughout the lifetime of the entire app).

The role of Context as an object?

Android Platform Architecture

What is Android Context — TL;DR

Here is a nice overall video on Android Context

Visualizing the Manifest & the Context

Each wire is a Context that connects the Application Layer with the Application Framework, opening a window between the connected component with the Android System
  1. The base is the Application Framework where all wires that connect every application component with the Android System emerge from.
  2. Each application through its manifest declarations exposes plug-holes for every declared component to the Android System, so it can construct them and put a context wire in order to manage them.
  3. And finally, each wire is the Android Context part of the constructed launchable component which binds that application component with the Android System.

Testing & Design Patterns

The significance of Context

--

--

MSc Computer Science. — Software engineer and programming instructor. Actively involved in Android Development and Deep Learning.

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Ioannis Anifantakis

Ioannis Anifantakis

MSc Computer Science. — Software engineer and programming instructor. Actively involved in Android Development and Deep Learning.