Skip to the workspace
Pebellslearn by building
Activities and Their Lifecycle  ›  Lesson

Activity — One Screen, Run by the System

Activity 12 minAn activity is an app component that owns one window of UI; the system creates, shows and destroys it, and your code reacts through callbacks.

android / Activity — One Screen, Run by the System

Explore each scene at your own pace.

01 · The rule

The rule

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:

  1. It is declared in the manifest. If it is not listed, the system refuses to start it.
  2. 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.
  3. You react through callbacks. The system calls methods such as onCreate, onStart and onResume, 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.

Scene 1 of 5
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.