What is an Activity and what is its lifecycle?
Tier: EssentialDifficulty: Easy
An Activity is a single screen with a UI, and its lifecycle is the set of callback methods the system calls as that screen moves between being created, visible, focused, backgrounded, and destroyed.
There are seven you should know cold.
onCreate()runs once, when the activity is first created. That's where you inflate the layout, set up your ViewModel, and restore any saved state.onStart()fires when the activity becomes visible.onResume()fires when it gains focus and the user can actually interact with it. This is the foreground state.onPause()fires the moment the user starts leaving. It has to be fast, because the next screen is waiting on it.onStop()fires once the activity is no longer visible at all. That's where you do heavier cleanup.onRestart()fires when a stopped activity is about to become visible again, right beforeonStart(). It's the one people forget, because it only runs on the way back and never on first launch.onDestroy()fires when the activity is finishing or being recreated, for final cleanup.
onPause runs when focus is lost, onStop when the activity is no longer visible, and onDestroy when it is finishing. A configuration change destroys the instance and recreates it from onCreate, which is why state that must survive rotation lives in a ViewModel.The rule of thumb interviewers are checking for is symmetry. Whatever you start in onResume(), like a camera preview or a sensor listener, you release in onPause(). Whatever you start in onStart(), you release in onStop().
One trap worth mentioning. A configuration change, like a screen rotation, destroys and recreates the activity by default, running the whole onPause through onDestroy then onCreate through onResume sequence again. There is no onRecreate() callback, which people sometimes expect. recreate() is a method you call to trigger that cycle yourself, not something the system calls on you. That's exactly why ViewModel exists, to hold UI state across that destroy and recreate cycle without you having to manually save and restore everything in onSaveInstanceState().
The other callback worth knowing is onNewIntent(), which sits outside that sequence. If an Activity is launched again while an instance already exists, and its launch mode is singleTop, singleTask or singleInstance, Android does not build a second instance and does not call onCreate() again. It calls onNewIntent() on the existing instance, then onRestart(), onStart() and onResume() if it had been stopped.
The gotcha interviewers like here is that getIntent() still returns the original Intent after onNewIntent() runs. You have to call setIntent(intent) yourself inside onNewIntent(), otherwise you keep reading stale data from whenever the Activity was first launched. This is the classic bug behind a notification tap that opens the wrong screen.
The scenario questions interviewers ask
Most of this topic gets asked as small puzzles. Here is the trigger and the exact callback order for the ones that keep coming up.
- A starts B. A
onPause, then BonCreate,onStartandonResume, and only after that AonStop. A stops last on purpose, because the system waits until B is actually on screen before it tears down what A was holding. That is also whyonPausehas to be tiny. B is waiting on it, so a disk write or a network call there turns into visible jank on every navigation. - Back from B to A. B
onPause, then AonRestart,onStartandonResume, then BonStopandonDestroy. Same rule, the leaving screen finishes last. A is the same instance, so there is noonCreate. - Home on A.
onPauseandonStop, withonSaveInstanceStatenext to them. On API 28 and later it runs afteronStop, and before API 28 it ran beforeonStop. Coming back from the launcher gives youonRestart,onStartandonResume, with noonCreate, because the instance was never destroyed. - A dialog themed or transparent activity on top of A. A gets
onPauseand nothing more. It is still partly visible, so it never reaches the stopped state. A plainDialog, the kind you show from the activity itself, is only a window inside that same activity, so it fires no lifecycle callback at all. People get that second half wrong constantly. - Rotation on A.
onPause,onStop,onSaveInstanceState,onDestroy, thenonCreatewith the saved Bundle,onStart,onRestoreInstanceState,onResume. A ViewModel survives, because the system keeps it as the retained non configuration instance and hands it to the new activity. Fields on the activity do not survive.android:configChangesis the opt out, and it is discouraged, because you then owe correct handling of every change you claimed and recreation can still happen for other reasons. - Process death in the background, then return. You come back on a brand new process, so
onCreateruns with a non null Bundle. The ViewModel is gone, since it only ever lived in memory. What comes back is whatever you wrote inonSaveInstanceStateor in aSavedStateHandle, which is the whole reasonSavedStateHandleexists. - Multi window or split screen. Before Android 10 only one activity is resumed at a time and the rest sit in the started state. On Android 10 and later, multi resume keeps both resumed, and
onTopResumedActivityChanged()is the callback that tells you which one currently has focus. That is where you grab or release a singleton resource like the camera or the microphone. - An incoming call or the lock screen on A. The full screen case is
onPausethenonStop, andonRestart,onStart,onResumewhen the user comes back. A call shown as a heads up overlay is the other case. A stays partly visible, so it only pauses, which is exactly the dialog themed activity rule again. finish()called insideonCreate.onDestroyruns andonStartandonResumenever do. The activity is finishing before it was ever visible, so nothing you set up for the visible states gets torn down for you. Clean up whatever you already allocated.- A notification opens A while B is on top. With
standard, you get a second A instance stacked above B. WithsingleTop,singleTaskorsingleInstance, the existing A getsonNewIntent(), thenonRestart,onStartandonResumeif it was stopped, and noonCreate. The full set of stack puzzles lives in launch modes.
Three of those, drawn out. Time runs left to right and the arrow follows the order the system actually calls things.
What all of these are really checking is three habits. That you treat onPause as a place for tiny work only, that onStop is where you release what the screen was holding, and that onDestroy is not guaranteed at all when the system kills your process. If cleanup has to happen, it belongs in onStop, not in onDestroy.