The basic Activity lifecycle is straightforward in theory, but in practice, mobile environments are hostile. The user rotates the phone, answers a phone call, or leaves the app in the background while playing a heavy 3D game.
Android's default behavior during significant environmental changes is highly aggressive: it destroys and completely recreates the active component.
Configuration Changes
The most notorious lifecycle event is the Configuration Change.
If a user rotates their phone from portrait to landscape, the physical screen dimensions change. Android assumes your app needs to load an entirely different XML layout file to accommodate this. Therefore, it instantly calls onDestroy() on your current Activity and creates a brand new instance with onCreate().
If you do not handle this properly, any data the user typed into a text box or any network request currently running will be instantly destroyed and lost.
Handling State Loss (Legacy)
Historically, developers used the onSaveInstanceState() bundle. Right before destruction, the OS asks the Activity to serialize small primitive data (like an integer ID or a short string) into a Bundle. When the new Activity is created, the Bundle is passed back.
// Legacy approach to saving state
@Override
protected void onSaveInstanceState(Bundle outState) {
outState.putString("USER_TEXT", mEditText.getText().toString());
super.onSaveInstanceState(outState);
}
The Modern Solution: ViewModel
Serializing data into Bundles is tedious and cannot handle complex objects or active network connections.
To solve this, Google introduced the ViewModel architecture component. A ViewModel is a special class that is intentionally scoped outside the immediate Activity lifecycle. When the phone rotates and the Activity is destroyed, the ViewModel is completely untouched in memory. When the new Activity instance is created, it simply reconnects to the exact same ViewModel instance.
ProcessLifecycleOwner
While understanding a single screen's lifecycle is important, apps often need to know when the entire application goes into the background (e.g., to pause a WebSocket connection).
The ProcessLifecycleOwner provides a composite lifecycle for the whole app. It considers the app "in the foreground" if at least one Activity is visible.
- It dispatches
ON_STARTandON_RESUMEinstantly when the first Activity appears. - It dispatches
ON_PAUSEandON_STOPwith a slight delay when the last Activity disappears (to account for brief transitions like rotating the device, ensuring it doesn't trigger false "background" events).
# Developers can forcefully trigger an aggressive background app kill to test lifecycle resilience
adb shell am kill com.example.myapp