Intents are how Android components talk — to launch another screen in your app, to pass data, or to ask the whole system to handle an action like sharing or opening a URL. Learn explicit and implicit intents.
An Intent is a message describing an operation to perform. An explicit intent names the exact component to start — used to open another Activity within your own app. You create the intent with the target class and call startActivity, and the system brings that screen forward and adds it to the back stack so the Back button returns.
import android.content.Intent
class MainActivity : Activity() {
fun openDetails() {
// explicit: name the exact Activity to start (within your app)
val intent = Intent(this, DetailActivity::class.java)
startActivity(intent) // system starts it + adds to the back stack
}
}
// Back button pops the back stack -> returns to MainActivity. The system
// manages this stack of screens for you (the "tasks & back stack").Intents carry data as extras — key-value pairs attached to the intent. The launching screen puts extras in, and the started Activity reads them from its intent. Keep extras small (an id, a flag); large objects belong in a shared data layer, not shuttled through intents. This is the basic mechanism for parameterizing a screen.
// sender: attach data as "extras"
val intent = Intent(this, DetailActivity::class.java).apply {
putExtra("userId", 42)
putExtra("name", "Ada")
}
startActivity(intent)
// receiver (DetailActivity.onCreate): read the extras back
val userId = intent.getIntExtra("userId", -1) // 42
val name = intent.getStringExtra("name") // "Ada"
// keep extras SMALL (ids, flags). Pass large data via a shared repository
// (Architecture course), not through intent extras.An implicit intent describes an action without naming a component, and Android finds an app that can handle it — open a URL, share text, dial a number, pick a photo. This is the mechanism behind the share sheet and "open with" chooser. It lets your app cooperate with the wider system instead of reimplementing everything itself.
// open a web page — the system picks a browser
val view = Intent(Intent.ACTION_VIEW, Uri.parse("https://belinkee.com"))
startActivity(view)
// share text — the system shows the share sheet of capable apps
val share = Intent(Intent.ACTION_SEND).apply {
type = "text/plain"
putExtra(Intent.EXTRA_TEXT, "Check this out!")
}
startActivity(Intent.createChooser(share, "Share via"))
// you describe the ACTION; Android finds an app that can do it.When you launch a screen or system action to get something back — a photo, a contact, a confirmation — you use the Activity Result APIs: register a launcher with a contract, launch it, and receive the result in a callback. This modern approach replaces the old onActivityResult and is how you integrate the camera, file picker, and permission requests.
class MainActivity : ComponentActivity() {
// register a launcher for a "take a picture" contract
private val takePhoto = registerForActivityResult(
ActivityResultContracts.TakePicturePreview()
) { bitmap ->
// callback with the result when the camera returns
if (bitmap != null) showImage(bitmap)
}
fun onCameraClick() {
takePhoto.launch(null) // launch the camera; result arrives above
}
}
// the modern Activity Result API — used for camera, file picker, permissions.