DevBits
DevBitsAndroid Handbook
Android Fundamentals

What is Context in Android?

Explain what Context gives an Android component and how to choose the right one.

AF-002Foundational~30 sec

What the interviewer is testing

The interviewer is checking whether you understand that Context is not just a parameter passed into Android APIs. It represents access to an Android-managed environment, and different Context implementations have different capabilities, configuration, and lifetimes.

A strong answer explains the choice in terms of capability and lifetime: what operation needs to be performed, and how long the object holding the Context will live.

❌ Common mistake

If they ask you to elaborate

A useful mental model is that Context is a gateway to Android-managed capabilities. Through it, code can access resources, files, databases, preferences, system services, component launching, and broadcasts.

In interviews, the practical decision usually comes down to Activity Context versus Application Context:

SituationRecommended ContextWhy
Inflate themed UIActivityUses the current screen's theme and configuration
Show an AlertDialogActivityRequires a window owned by the current Activity
Navigate to another screenActivityLaunches UI from the current screen and task
Analytics singletonApplicationLives independently of any Activity
Repository or database helperApplicationLong-lived and not tied to a screen
Read app resourcesUsually eitherChoose based on the lifetime of the caller

The rule is not "always use Application Context." Use the narrowest Context that supports the operation, and never let a short-lived Context outlive its owner.

Code example

class AnalyticsClient(context: Context) {
    // This object may live for the entire app process,
    // so it must not retain an Activity.
    private val appContext = context.applicationContext

    fun appPackage(): String = appContext.packageName
}

class DetailsActivity : AppCompatActivity() {
    fun showDeleteConfirmation() {
        AlertDialog.Builder(this) // Activity Context: themed UI + window
            .setTitle(R.string.delete_title)
            .setMessage(R.string.delete_message)
            .setPositiveButton(R.string.delete) { _, _ -> deleteItem() }
            .setNegativeButton(R.string.cancel, null)
            .show()
    }

    private fun deleteItem() = Unit
}

The analytics client converts the supplied Context to applicationContext because it may outlive the screen. The dialog uses the Activity Context because it needs the Activity's theme and window.

Follow-up questions

What separates a senior answer

Key takeaways

Learn more

On this page