Android from the foundation · Puzzle 3 of 16

Who decides about your screens

Your app doesn't decide when its own screens appear or go away. A separate Android process does, and tells your app.

The problem

In puzzle 2, the main thread drew your app's screen. But many apps are running, and only one can be on screen. Someone has to decide which screen is in front, which ones are covered or hidden, and which ones are closed. That can't be each app for itself.

See it

The town hall (system_server) keeps a register and moves your app's screen along the lifecycle: created, started, resumed, paused, stopped, destroyed kernel your app screen stopped system_server createdstartedresumedpausedstoppeddestroyed resume stop destroy
  1. Each screen of your app is an Activity. Your app shows one now.

    Step 1 of 5
  2. Android runs a process of its own, system_server: the town hall. It keeps the register of every Activity of every app.

    Step 2 of 5
  3. It moves each Activity along a path of stages, the lifecycle: created, started, resumed. Resumed means in front and ready for taps. Predict: what happens when the user switches to another app?

    Step 3 of 5
  4. system_server sends your app a message each time (puzzle 4 shows how): your Activity moves to paused (no longer in front), then stopped (hidden). Your app doesn't choose this; it's told.

    Step 4 of 5
  5. When the screen is closed for good, system_server moves it to destroyed.

    Step 5 of 5

The building block

In Android, each screen of an app is an Activity.

Android runs one long-lived process of its own, system_server. It keeps a register of every Activity of every app, and it decides what happens to each one.

system_server moves each Activity along a fixed path of stages, called the lifecycle, and your app is told about every move:

  • created: the screen is set up, not yet visible;
  • started: visible;
  • resumed: in front, ready for taps;
  • paused: still visible, but no longer in front (a dialog covers it);
  • stopped: not visible anymore (another app is in front);
  • destroyed: gone for good.

The analogy

system_server is the town hall. It keeps the official register of every house and every room, and it decides which room is open. The houses don't decide for themselves; they get told.

Check yourself

The user switches from your app to another app. Who moves your Activity to "stopped"?

Not quite. The kernel shares the CPU and memory; screens and their stages are system_server's job.

Not quite. Your app finds out, but it doesn't make the decision. The town hall moves it.

Right. system_server decides every lifecycle stage; your app is told.

Why can't each app decide on its own which screen is in front?

Not quite. Apps have plenty of screens. The question is who decides which one is in front.

Right. Only something that sees every app can fairly decide which screen is in front.

What's the difference between an Activity and system_server?

Not quite. An Activity lives in your app's process; system_server is a separate process: the town hall.

Right. Your app holds the screens; the town hall holds the register and the decisions.

Sources

What you now know

system_server is the town hall. It keeps the register of every app's screens and moves each screen through its lifecycle. Your app follows.