Android Intents and Component Communication
Android Intents and Component Communication
An Intent in Android is a messaging object that describes an operation to perform. It serves as the primary mechanism for communication between application components, whether those components live in the same process or in different applications entirely. Intents carry both the action to perform and the data to operate on, and the system uses this information to determine which component should handle the request. By the end of this lesson, you will be able to:
- Explain the role of Intents in Android's component based architecture.
- Distinguish between explicit and implicit Intents with respect to component resolution.
- Describe how the system resolves an implicit Intent using intent filters.
- Identify the data an Intent can carry through extras, URIs, and categories.
- Apply best practices for securing Intent based communication.
Explicit Intents and Direct Component Targeting
An explicit Intent specifies the exact component that should handle it by providing the target class name. The system does not need to search for a matching component because the caller has already identified the destination:
val intent = Intent(this, DetailActivity::class.java)
intent.putExtra("item_id", 42)
startActivity(intent)
The ComponentName is set internally when you pass the Context and Class to the constructor. The system reads this component name, locates the activity in the manifest, and starts it directly. No resolution step is involved.
Explicit Intents are the standard mechanism for navigating between activities within a single application. Because the caller names the target directly, there is no ambiguity about which component will handle the request. This also means explicit Intents cannot cross application boundaries unless you know the exact class name in the other application, which is rarely practical and tightly couples the two apps.
Implicit Intents and Intent Resolution
An implicit Intent does not name a specific component. Instead, it declares an action and optionally includes data, a MIME type, and categories. The system examines the intent filters declared in the manifests of all installed applications and finds components that match:
val intent = Intent(Intent.ACTION_VIEW)
intent.data = Uri.parse("https://developer.android.com")
startActivity(intent)
The resolution process matches three elements. The action string must match an action listed in the intent filter. The data URI scheme, authority, and path must match the filter's data specification. The category must be present in the filter, and CATEGORY_DEFAULT is automatically added to every implicit Intent passed to startActivity().
If multiple components match, the system shows a chooser dialog letting the user select which app should handle the request. If no component matches, the system throws an ActivityNotFoundException. Checking resolveActivity() before calling startActivity() prevents this crash:
val intent = Intent(Intent.ACTION_SEND).apply {
type = "text/plain"
putExtra(Intent.EXTRA_TEXT, "Hello")
}
if (intent.resolveActivity(packageManager) != null) {
startActivity(intent)
}
This interview continues for subscribers
Subscribe to Dove Letter for full access to exclusive interviews about Android and Kotlin development.
Become a Sponsor