Process Death

Process Death and Saving Instance State

Once an activity is stopped, Android may kill its process at any moment to free memory, and no callback runs. When the user returns through Recents or the launcher, the system starts a new process, recreates the activity and hands onCreate the Bundle it collected from onSaveInstanceState before the kill. rememberSaveable writes into that Bundle; remember and a ViewModel die with the process. am kill reproduces this, since it kills only a process with nothing in the foreground, as the low-memory killer would:

Simulating process death: Home, kill, relaunchShell
adb shell input keyevent KEYCODE_HOME
adb shell am kill com.example.booknest
adb shell am start -a android.intent.action.MAIN \
  -c android.intent.category.LAUNCHER -n com.example.booknest/.MainActivity
Output
09-24 18:37:16.909 D/BookNestLife( 9238): onPause
09-24 18:37:19.970 D/BookNestLife( 9238): onStop
09-24 18:37:19.970 D/BookNestLife( 9238): onSaveInstanceState
09-24 18:37:29.995 D/BookNestLife( 9585): onCreate restored=true
09-24 18:37:30.048 D/BookNestLife( 9585): onStart
09-24 18:37:30.056 D/BookNestLife( 9585): onResume

The process ID changed from 9238 to 9585, yet restored=true: the cart count came back as 3 and the tap count restarted at 0 (see the figure in Configuration Changes). Relaunch with the launcher's intent; a different one stacks a fresh MainActivity on the old task. The Bundle crosses Binder's 1 MB per-process buffer, so keep it to IDs, text and scroll positions (Google suggests under 50 KB) and reload the rest from Room 234 (Local Storage with Room).