Android from the foundation · Puzzle 5 of 16

You don't call. You're called.

Put the characters you've met together, and you get the one chain behind all of Android's lifecycle code.

The problem

You've met the five characters: your app's process (puzzle 0), the kernel (1), the main thread (2), system_server (3) and Binder (4). Now: when the town hall's message arrives, what actually runs in your app?

See it

The town hall (system_server) writes a letter; the post office (Binder, in the kernel) carries it into your app's house; the clerk (main thread) takes it from the in-tray and runs onCreate() kernel Binder your app onCreate() { … } main thread system_server create X runs!
  1. Your app's process, with the main thread at its desk. On the desk: your onCreate()Your override of this method is what the main thread calls when system_server says "create". 1864 protected void onCreate(@Nullable Bundle savedInstanceState) {Activity.java, line 1864 · AOSP frameworks/base ↗, the method that sets up a screen, waiting.

    Step 1 of 5
  2. The town hall, system_server, decides: "create screen X". It writes the message.

    Step 2 of 5
  3. Binder, the post office in the kernel, carries it. Predict: where does it land?

    Step 3 of 5
  4. On the main thread's desk (by a route puzzle 14 shows). The main thread handles one message at a time, in order.

    Step 4 of 5
  5. The main thread runs your code: onCreate()Your override of this method is what the main thread calls when system_server says "create". 1864 protected void onCreate(@Nullable Bundle savedInstanceState) {Activity.java, line 1864 · AOSP frameworks/base ↗. You wrote it; you never called it. It's a callback.

    Step 5 of 5

The building block

A callback is a method you write so that someone else can call it when the time comes. Lifecycle code is all callbacks: onCreate()Your override of this method is what the main thread calls when system_server says "create". 1864 protected void onCreate(@Nullable Bundle savedInstanceState) {Activity.java, line 1864 · AOSP frameworks/base ↗ sets up an Activity, and there's one callback for each lifecycle stage.

You write them. You never call them yourself. system_server decides, Binder delivers, and your main thread runs your callback. If you did call onCreate()Your override of this method is what the main thread calls when system_server says "create". 1864 protected void onCreate(@Nullable Bundle savedInstanceState) {Activity.java, line 1864 · AOSP frameworks/base ↗ by hand, it would only run your method: system_server's register wouldn't change, and no screen would appear.

(This picture is simplified: puzzles 13 and 14 show how the message gets from Binder to the main thread.)

The analogy

You leave instructions on the clerk's desk ("when the town hall says open, do this"). The clerk follows them when the letter arrives. You never knock on the town hall's door asking to be opened.

Check yourself

Your app calls onCreate()Your override of this method is what the main thread calls when system_server says "create". 1864 protected void onCreate(@Nullable Bundle savedInstanceState) {Activity.java, line 1864 · AOSP frameworks/base ↗ on its own Activity, by hand. What have you done?

Right. A callback is run by your main thread when system_server says it's time. Calling it yourself doesn't change what system_server decided.

Not quite. Only system_server moves an Activity through its lifecycle. Calling the callback yourself just runs your method.

Which thread runs your onCreate()Your override of this method is what the main thread calls when system_server says "create". 1864 protected void onCreate(@Nullable Bundle savedInstanceState) {Activity.java, line 1864 · AOSP frameworks/base ↗?

Right. The message reaches your main thread, which runs your callback.

Not quite. Screen code always runs on the main thread, the clerk.

Not quite. system_server only sends the message. Your code runs in your process, on your main thread.

Why does Android use callbacks instead of letting apps decide?

Not quite. Speed isn't the point: who decides is.

Right. The town hall decides when; your callback says what.

Sources

What you now know

system_server decides → Binder carries the message → your main thread receives it → your code runs. You write onCreate(); you never call it.