Explicit Intents — Opening Your Own Screens
The user taps "Monstera" in Sprout's list. The detail screen must open, and it must know which plant to show. Both jobs are done by one small object: an intent.
The rule
Name the class, pack the extras, ask the system.
An intent is a message asking the system to do something, such as start an activity. An explicit intent names the exact component, so there is no doubt who receives it. You use it for screens inside your own app.
// In the list
val intent = Intent(this, PlantDetailActivity::class.java)
intent.putExtra("plant_id", plant.id)
startActivity(intent)// In PlantDetailActivity.onCreate
val plantId = intent.getLongExtra("plant_id", -1L)- Name the class —
PlantDetailActivity::class.javais the target. - Pack the extras —
putExtra(key, value)attaches small values: numbers, strings, booleans. - Ask the system —
startActivityhands the intent to Android, which starts the activity.
The receiver reads extras with a matching getter and a default value returned when the key is missing. Keep keys in a shared constant so the two sides cannot drift apart.
Extras should stay small. They travel through the system and have a size limit, so send an ID, not a photo. The receiving screen loads the full data itself.
The shortcut
Say it as a sentence: "Start this class, carrying this key and value." If you cannot fill both blanks, the intent is not ready.
Name the class
Target:
PlantDetailActivity::class.java.
If the keys differ, say "plant_id" on one side and "plantId" on the other, nothing crashes: the getter silently returns the default. That is why shared constants matter.
Drills
Answer fast — trust the rule.
What makes an intent explicit?
Which call attaches the plant ID to the intent?
The sender used key "plantId" but the receiver reads getLongExtra("plant_id", -1L). What happens?
Why send a plant's ID rather than its full-size photo as an extra?
Remember
- Name the class, pack the extras, ask the system.
- Receivers read extras with a matching getter and a default.
- Send IDs, not big objects, and share key constants.
