Activity — One Screen, Run by the System
Sprout will have a plant list and a plant detail screen. In classic Android each of those screens is an activity. The most important thing to understand about activities is who is in charge of them. It is not you.
The rule
An activity is a screen the system runs; you only answer its calls.
An activity is one of Android's four app components. It owns a window and draws UI into it. Three facts define it:
- It is declared in the manifest. If it is not listed, the system refuses to start it.
- The system creates it. You never write
MainActivity()yourself. You ask the system to start it with an intent, and the system builds the object. - You react through callbacks. The system calls methods such as
onCreate,onStartandonResume, and you override them to do your work.
class PlantDetailActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) // always call super first
setContent { PlantDetailScreen() }
}
}ComponentActivity is the base class used with Compose. Older View-based apps usually extend AppCompatActivity, which is built on top of it.
Modern apps often use one activity and switch between screens with Compose navigation inside it. The activity is still the container the system runs; it just hosts many screens.
The shortcut
Before writing activity code, ask "am I doing this, or is the system?" Creating, showing, hiding and destroying: the system. Filling the screen and reacting: you.
You ask
Your code calls
startActivity(...)with an intent forPlantDetailActivity.
Drills
Answer fast — trust the rule.
How should you open a new activity?
Which base class is typically used for an activity that shows Compose UI?
What must you do in an overridden onCreate before your own code?
Which of these is one of Android's four app components?
Remember
- An activity is a screen the system runs; you only answer its calls.
- Declared in the manifest, created by the system, driven by callbacks.
- Never construct an activity or call its lifecycle methods yourself.
