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.
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"
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)
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)
)
}
}
Broaden your foundational and advanced Android application development skills across our curriculum:
Launch activities and handle ActivityResultContracts safely.
Broadcast intents across application and OS boundaries.
Configure intent-filter elements to catch incoming web URLs.
Launch system permission request intents with registerForActivityResult.