Sitemap
Manish Mishra

Manish Mishra

Hi, I’m Manish Mishra, a mobile development specialist with over 5 years of experience building apps for iOS and Android.

Context vs ApplicationContext in Android: What’s the Difference and When to Use Each

3 min readMay 2, 2025

--

Press enter or click to view image in full size

I’m Manish Mishra, founder of GeekyMob.com and a mobile development specialist with 5+ years of experience building apps for iOS and Android. I started this blog to share my knowledge, help developers master mobile technologies, and stay updated with the latest best practices.

If you’re new to Android development, one term you’ll hear often is Context. It shows up everywhere — when displaying a Toast, starting a new Activity, accessing resources, or initializing system services.

Get Manish Mishra’s stories in your inbox

Join Medium for free to get updates from this writer.

But what is Context, and why are there two types: Activity Context and Application Context?

Why are there so many contexts in my life?!

Let’s understand both in a clear and simple way.

What is Context?

In Android, Context is an interface to global information about your application environment. It lets your app

  • Access resources (like strings, layouts, drawables)
  • Show UI elements (like Toasts and dialogs)
  • Launch activities or services
  • Access system services (like Location, Clipboard, Notification)

In short: Context is the middleman between your app and Android's internals.

Types of Context

There are two main types of Context:

1. Activity Context

  • Tied to the lifecycle of a specific Activity
  • Used for anything related to UI or the current screen
  • Gets destroyed when the Activity is destroyed

Use cases:

  • Displaying a Toast
  • Showing a Dialog
  • Inflating Views
  • Starting another Activity

Example:

Toast.makeText(this, "Welcome!", Toast.LENGTH_SHORT).show()

Here, this refers to the Activity Context.

2. Application Context

  • Tied to the lifecycle of the entire application
  • Lives as long as your app is running
  • Not tied to any one Activity or screen

Use cases:

  • Initializing singletons
  • Accessing shared preferences
  • Starting long-running background tasks
  • Using dependency injection (e.g., Hilt)

Example:

val appContext = applicationContext

You can also inject it using Hilt:

@HiltViewModel
class MyViewModel @Inject constructor(
@ApplicationContext val context: Context
) : ViewModel()

When to Use Which?

Press enter or click to view image in full size
Here’s a simple comparison chart b/w context and applicationContext

Why It Matters

Using the wrong Context can cause:

  • Memory leaks: If you store an Activity Context in a long-lived object (like a ViewModel or Singleton), the Activity can’t be garbage collected.
  • Crashes: Some operations like showing dialogs require Activity Context. Using Application Context may cause runtime errors.

Rule of Thumb

Use Activity Context for UI-related operations.
Use
Application Context for app-wide, background, or long-lived operations.

If you’re unsure, use the Context that’s available closest to where you are in the code. Only switch to applicationContext when you’re sure the reference will outlive the current Activity.

Final Thoughts

Understanding Context helps you write cleaner, safer Android code. It avoids memory leaks and ensures proper behavior of system operations.

As a beginner: Stick with Activity Context by default. Move to Application Context only when needed.

If this post helped clear up the confusion about Context, share it with your developer friends — it might save them some serious head-scratching too!

Got questions or stories to share? Drop a comment or reach out to me on GeekyMob.com — I’d love to hear from you!

--

--