Activity Lifecycle Flashcards

ANDROID › Lifecycle

What is the full ordering of the core Activity lifecycle callbacks?
Entering foreground: onCreate then onStart then onResume. Leaving: onPause then onStop then onDestroy. They are mirrored pairs around the Resumed state, with onRestart bridging onStop back to onStart.
Which callbacks run on a cold start versus returning to a stopped (not destroyed) activity?
Cold start: onCreate, onStart, onResume. Returning from Stopped: onRestart, onStart, onResume. No onCreate runs and savedInstanceState is irrelevant because the instance was never destroyed.
When are onSaveInstanceState and onRestoreInstanceState called, and when are they not?
They run only when the system may destroy the activity to recreate it later (configuration change or process death), saved sometime before onStop. Restore happens in onCreate via the Bundle or in onRestoreInstanceState after onStart. Neither runs on Back or finish().
What happens if you call finish() inside onCreate()?
Documented exception: the system skips onStart, onResume, onPause, and onStop and calls onDestroy() immediately. The sequence is just onCreate then onDestroy.
In multi-window mode, why prefer onStop over onPause to release UI resources?
An unfocused activity is Paused but can still be fully visible in multi-window. Tearing down UI resources in onPause would blank a visible window, so release them in onStop, which only runs when the activity is truly invisible.
What is onTopResumedActivityChanged(Boolean) and why does it exist?
Added in API 29 for multi-resume: several activities can be RESUMED at once, but only one is the top-focused one. The callback signals which activity currently owns exclusive singleton resources like the camera or microphone.
Inside onDestroy(), how do you tell a real finish from a config-change recreation?
Call isFinishing(). true means the activity is genuinely finishing (Back or finish()); false means it is being destroyed transiently to be recreated, such as on rotation. ViewModels survive the latter but are cleared on the former.
What callbacks fire when Activity A launches a full-screen Activity B?
A.onPause, then B.onCreate, B.onStart, B.onResume, then A.onStop. A pauses before B is created, and A only stops once B is fully visible and covers it.
How does launchMode singleTop change the lifecycle when the target is already at the top of the stack?
No new instance and no onCreate. The existing top instance receives onNewIntent(), typically onPause, onNewIntent, then onResume. The same applies with FLAG_ACTIVITY_SINGLE_TOP.

Back to Activity Lifecycle