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
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 5The town hall, system_server, decides: "create screen X". It writes the message.
Step 2 of 5Binder, the post office in the kernel, carries it. Predict: where does it land?
Step 3 of 5On 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 5The 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
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.
Answer every question above correctly to unlock the next puzzle.