Activity Lifecycle Interview Questions
ANDROID › Lifecycle
Walk me through the Activity lifecycle callbacks in order.
What a strong answer covers: onCreate (set up the UI, ViewModel, restore state), onStart (visible), onResume (in the foreground and interactive), then onPause (losing focus, partly visible), onStop (no longer visible), onDestroy (finishing or being recreated). onRestart runs before onStart when coming back from stopped. A strong answer says what belongs in each: start camera or sensors in onStart/onResume and release them in the matching onPause/onStop.
Which callbacks fire when you rotate the device, press Home, press Back, or a dialog appears?
What a strong answer covers: Rotation: onPause, onStop, onSaveInstanceState, onDestroy, then a new instance's onCreate, onStart, onRestoreInstanceState, onResume. Home: onPause, onStop, onSaveInstanceState. Back on the root activity: onPause, onStop, onDestroy (since Android 12 the root launcher activity is moved to the back instead of finished). A dialog inside your app doesn't pause the Activity at all; a translucent activity or system dialog on top calls onPause only.
What's the difference between onPause and onStop, and what work belongs in each?
What a strong answer covers: onPause means the activity is partly visible or losing focus (multi-window, a translucent activity on top) and should be fast; in multi-window the paused activity can still be visible, so don't stop visible UI work there. onStop means it's fully hidden: release heavier resources, stop animations and location updates, and save data. Since Android 10 multi-resume, several activities can be resumed at once in multi-window.
When is onSaveInstanceState called, and what should and shouldn't go in the Bundle?
What a strong answer covers: It's called before the activity may be destroyed by the system (after onStop on API 28+, before it on older versions), but not when the user explicitly finishes it with Back. Put small, transient UI state there: a scroll position, a selected tab, an entered search query, an id to reload. Don't put large data or bitmaps: the bundle goes through Binder, which is limited to about 1 MB shared per process, and too much causes TransactionTooLargeException.
How do you test that your app handles the Activity being destroyed and recreated correctly, including process death?
What a strong answer covers: For configuration changes: rotate, change dark mode or font size, or use ActivityScenario.recreate() in tests. For process death: put the app in the background and run adb shell am kill <package>, or turn on 'Don't keep activities' in developer options (which only covers activity destruction, not process death). Check that UI state restores from SavedStateHandle or rememberSaveable and that ViewModels reload data. A strong answer distinguishes the two cases.