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.