Android Intents: Explicit, Implicit & Activity Result Contracts

An Intent is an asynchronous messaging object used to request an action from another application component. Intents serve as the fundamental glue of Android, facilitating three primary use cases: starting an Activity, starting a Service, and delivering a system broadcast.

1. Explicit Intents (Internal Navigation)

An Explicit Intent explicitly specifies the target component by its exact class name. Explicit intents are typically used to navigate between screens inside your own application package:

import android.content.Intent

// Inside an Activity or Composable with LocalContext.current:
val intent = Intent(this, DetailActivity::class.java).apply {
    putExtra("EXTRA_TUTORIAL_ID", 101)
    putExtra("EXTRA_TITLE", "Kotlin Coroutines Masterclass")
}
startActivity(intent)

// Inside the receiving DetailActivity:
val tutorialId = intent.getIntExtra("EXTRA_TUTORIAL_ID", -1)
val title = intent.getStringExtra("EXTRA_TITLE") ?: "Default Title"

2. Implicit Intents (Invoking External Apps)

An Implicit Intent specifies a general action to be performed (such as viewing a webpage, making a phone call, or sharing text). The Android OS inspects the Intent Filters of all installed apps to find an app capable of handling the request:

import android.net.Uri

// 1. Open a Webpage in Chrome/Default Browser
val webIntent = Intent(Intent.ACTION_VIEW, Uri.parse("https://veewom.com/android/"))
startActivity(webIntent)

// 2. Dial a Phone Number
val dialIntent = Intent(Intent.ACTION_DIAL, Uri.parse("tel:+1234567890"))
startActivity(dialIntent)

// 3. Share Text via WhatsApp / Social Apps (Share Sheet)
val sendIntent = Intent().apply {
    action = Intent.ACTION_SEND
    putExtra(Intent.EXTRA_TEXT, "Learn Android at Veewom: https://veewom.com/android/")
    type = "text/plain"
}
val shareIntent = Intent.createChooser(sendIntent, "Share Tutorial via")
startActivity(shareIntent)

3. Modern Activity Result Contracts (Replacing startActivityForResult)

The legacy startActivityForResult() and onActivityResult() methods have been fully deprecated. Modern Android uses type-safe Activity Result Contracts:

import androidx.activity.ComponentActivity
import androidx.activity.result.contract.ActivityResultContracts

class ProfileActivity : ComponentActivity() {

    // Register contract at class initialization!
    private val pickPhotoLauncher = registerForActivityResult(
        ActivityResultContracts.PickVisualMedia()
    ) { uri: Uri? ->
        if (uri != null) {
            Log.d("PhotoPicker", "Selected photo URI: $uri")
            // Update user avatar
        }
    }

    private fun selectProfilePicture() {
        // Launch photo picker safely without requiring broad storage permissions!
        pickPhotoLauncher.launch(
            PickVisualMediaRequest(ActivityResultContracts.PickVisualMedia.ImageOnly)
        )
    }
}

Frequently Asked Questions (FAQ) - Student & Interview Review

Q1. What is an Intent Filter in Android?

An Intent Filter is an XML declaration inside `AndroidManifest.xml` that specifies the types of implicit intents an Activity, Service, or BroadcastReceiver can respond to based on action, data (MIME type or URI scheme), and category.

Q2. Why was startActivityForResult deprecated?

It required fragile integer request codes, caused monolithic switch/case blocks in `onActivityResult()`, and frequently caused crashes when the launching Activity was destroyed by the OS during memory pressure while the child activity was open.

Q3. What happens if no installed app can handle an implicit Intent?

If you call `startActivity(implicitIntent)` and no application on the device can handle the action, Android throws an `ActivityNotFoundException` and crashes. Always wrap implicit intents in a try/catch block or check with `resolveActivity()`.

Explore Related Android Development Classes & Next Steps

Broaden your foundational and advanced Android application development skills across our curriculum:

Activity Lifecycle & Results

Launch activities and handle ActivityResultContracts safely.

BroadcastReceivers & System Events

Broadcast intents across application and OS boundaries.

Deep Linking & App Links

Configure intent-filter elements to catch incoming web URLs.

Runtime Permissions & Contracts

Launch system permission request intents with registerForActivityResult.

Share Share on Facebook Share on Twitter Share on LinkedIn Pin on Pinterest Share on Reddit

You may also like this!