Android from the foundation · Puzzle 12 of 16
Starting an app, cold, warm and hot
Opening an app can mean very different amounts of work, depending on whether its process and its screen still exist.
The problem
In puzzles 10 and 11, a new app process was forked from Zygote. But is a process forked every time you open an app? Sometimes the app opens instantly, sometimes it takes a second. What's the difference?
See it
Opening an app can need three things: a process, one instance of your app's
Applicationclass (one per process, for setup the whole app needs), and the Activity on screen.Step 1 of 4Cold start: no process yet (never started, or Android ended it to free memory). Fork from Zygote, create the Application (its
Application.onCreate()runs), then the Activity. The slowest.Step 2 of 4Warm start: the process is alive, but the Activity was destroyed. Only the Activity is created again. Predict: what's left to do if both are alive?
Step 3 of 4Nothing to create. Hot start: system_server just brings the screen back to the front: started, then resumed.
Step 4 of 4
The building block
A process isn't kept forever: when memory runs short, Android ends the processes of apps that aren't on screen (in the background), so coming back to an app can need a fresh one.
Your app has one instance of its Application class per process: a place for setup the whole app needs, like opening the file where the app keeps its saved data. It's created right after the fork, and its callback Application.onCreate() runs before any Activity exists.
- Cold start: no process yet. Fork from Zygote, create the Application, then create the first Activity.
- Warm start: the process is still alive, but the Activity was destroyed (puzzle 3), for example because the user closed it, so it's created again.
- Hot start: process and Activity are both alive, stopped in the background; system_server just brings the screen back to the front.
The analogy
Cold: build the house from the show house, move in, set up the room. Warm: the house stands, set up the room again. Hot: everything's ready, just open the curtains.
Check yourself
Sources
What you now know
Cold start = fork Zygote, create your Application, then the first Activity. Warm = process alive, Activity created again. Hot = both alive, just brought to the front.
Answer every question above correctly to unlock the next puzzle.