What is Context in Android?
Explain what Context gives an Android component and how to choose the right one.
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:
| Situation | Recommended Context | Why |
|---|---|---|
| Inflate themed UI | Activity | Uses the current screen's theme and configuration |
Show an AlertDialog | Activity | Requires a window owned by the current Activity |
| Navigate to another screen | Activity | Launches UI from the current screen and task |
| Analytics singleton | Application | Lives independently of any Activity |
| Repository or database helper | Application | Long-lived and not tied to a screen |
| Read app resources | Usually either | Choose 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.