Context in Android

Burak Taşcı
3 min readJun 11, 2023

--

Every Android developer quickly realizes the significance of the Context class right from the start of their journey. The Context is like a key that unlocks a treasure trove of essential resources and services within an Android application’s environment. It acts as a bridge connecting developers to the Android system, empowering them to understand and interact with various components of the operating system.

From a developer’s perspective, the Context is their gateway to accessing vital app-specific information and system-wide functionalities. It enables them to tap into a wealth of resources that make their applications dynamic, visually appealing, and user-friendly.

What context can do by itself?

The Context class itself does not have any standalone functionality. It serves as a handle or reference to the Android system and provides access to various resources, services, and application-specific information. However, it is the methods and functionalities available through the Context class that allow developers to perform specific tasks.

Are there different kind of contexts and why?

The common actions you can safely take with a given Context object depends on where it came from originally. The reason for having different types of Contexts is to ensure that developers have the appropriate level of access and scope based on their needs. Using the correct type of Context is important because it helps manage the lifecycle and resources efficiently. It’s important for developers to understand the differences between these Context types and use them appropriately based on the specific requirements of their application. Below is a table of the common places an application will receive a Context, and in each case what it is useful for:

  1. An application CAN start an Activity from here, but it requires that a new task be created. This may fit specific use cases, but can create non-standard back stack behaviors in your application and is generally not recommended or considered good practice.
  2. This is legal, but inflation will be done with the default theme for the system on which you are running, not what’s defined in your application.
  3. Allowed if the receiver is null, which is used for obtaining the current value of a sticky broadcast, on Android 4.2 and above.

Some Usage Examples for Context in Kotlin

  1. Accessing Resources:
val appName = context.getString(R.string.app_name)
val textColor = context.getColor(R.color.text_color)
val icon = context.getDrawable(R.drawable.app_icon)

2. Starting Activities:

val intent = Intent(context, TargetActivity::class.java)
context.startActivity(intent)

3. Accessing System Services:

val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val audioManager = context.getSystemService(Context.AUDIO_SERVICE) as AudioManager

4. Creating Views:

val inflater = LayoutInflater.from(context)
val view = inflater.inflate(R.layout.layout_file, parent, false)
val textView = view.findViewById<TextView>(R.id.text_view)

5. Accessing Application-specific Information:

val packageName = context.packageName
val preferences = context.getSharedPreferences("my_prefs", Context.MODE_PRIVATE)

6. Accessing Device Display Metrics:

val displayMetrics = context.resources.displayMetrics
val screenWidth = displayMetrics.widthPixels
val screenHeight = displayMetrics.heightPixels

7. Loading and Manipulating Assets:

val assetManager = context.assets
val inputStream: InputStream = assetManager.open("file.txt")
val text = inputStream.bufferedReader().use { it.readText() }

8. Accessing Resources in a Different Package:

val otherPackageContext = context.createPackageContext("com.example.otherpackage", Context.CONTEXT_IGNORE_SECURITY)
val otherPackageName = otherPackageContext.packageName
val otherStringRes = otherPackageContext.getString(R.string.some_string)

Summary

In conclusion, the Context class serves as a bridge between Kotlin/Java files and the Android system in Android development. It provides access to resources, system services, and information within an application's environment. By utilizing different types of Contexts, developers can ensure the appropriate level of access and scope for their specific needs. The Context class enables developers to interact with various components of the Android system, facilitating the development of feature-rich and interactive Android applications.

Resources & References:

[1]: https://developer.android.com/reference/android/content/Context

[2]: https://web.archive.org/web/20150329210012/https://possiblemobile.com/2013/06/context/

[3]:https://github.com/codepath/android_guides/wiki/Using-Context

--

--