How to Test Android State Restoration After Process Death With ADB

Wait 5 sec.

Suppose an Android search screen keeps its query when you rotate the phone. The selected item stays highlighted, the results remain visible, and the test passes. Later, you leave the app, Android removes its background process, and returning to the same task opens an empty screen.The rotation test was useful. It just exercised a different lifetime.Activity recreation, process death, and task removal are separate experiments. Treating all three as “restart the app” makes a green restoration test difficult to interpret. A stronger test records what was destroyed, how the app was resumed, and which state should survive that particular transition.The search screen and package name below are illustrative. This is a test recipe for a single-process debug build, with expected outcomes rather than device measurements.Start with the lifetime, not the gestureAn Activity is one component of an app. A process contains its in-memory objects. A task holds a navigation stack. Destroying something at one level does not establish that the other levels were destroyed.That distinction matters because a ViewModel can remain in memory across a configuration change, but cannot survive the death of its process. Android’s UI-state guidance separates that memory from saved state and persistent storage.Use three different test names:Activity recreation: can the new Activity reconnect to the state it should retain?Background process death: can a new process reconstruct the screen when the existing task is restored?Task dismissal and a fresh launch: does the app start with the intended transient state while keeping data that was explicitly saved?The third test should not automatically expect the second test’s result.Write a restoration contract for one screenChoose a small, recognizable state. For the example search screen, enter restoration-check-42 and select a fixture item with ID item-17.Before touching a test runner, define the expected return experience. The query should remain visible after system-initiated process death. If the selected item still exists, the screen should resolve that ID again. Results may need to reload; an old list object does not have to survive.Also define the exception. If the fixture no longer contains item-17, show an unavailable-item state or clear the selection deliberately. Rendering a familiar label beside the wrong record would not be successful restoration.This gives the test something stronger than “the screen looks populated.” It must reconstruct the right state from the right source.Prove that the process actually changedA recreated Activity can look exactly like a restored process. Give the debug build an independent process marker.For example, add a random value to your existing Application subclass and log it from onCreate():import android.app.Applicationimport android.os.Processimport android.util.Logimport java.util.UUIDclass ProbeApplication : Application() { private val processInstance = UUID.randomUUID().toString() override fun onCreate() { super.onCreate() Log.i("RestoreProbe", "instance=$processInstance pid=${Process.myPid()}") }}If you create this class for a minimal sample, register it as the application class in the manifest. In an existing app, put the probe in its current Application implementation rather than replacing framework setup. Keep the marker in memory; do not restore it from saved state.A new marker establishes a new Application instance in this single-process setup. A new Activity alone does not. Keep the process evidence beside the UI assertions, so an unsuccessful kill cannot quietly become a passing recovery test.Kill the background process and resume the same taskInstall and open your debug app, record its process marker, and set the fixture state. Press Home and allow the Activity to reach its stopped state. Keep the app’s task in Recents.From a development machine with Android Platform Tools, target the appropriate device:adb devices# Replace the serial and package with your debug app's values.adb -s emulator-5554 shell am kill --user current com.example.restoreprobeAndroid documents the activity manager’s kill command as killing package processes that are safe to terminate. The command is not a guarantee that every process configuration will be eligible. A foreground service or another active component may change the experiment.Return through the app’s existing Recents entry. Check for a new process marker and then check the query, selected ID, and reconstructed result state. If there is no new marker, investigate whether the process survived. If the task is gone, you have not followed the same-task restoration path. Record either case as an invalid setup for this test, rather than a restoration success.Do not substitute an IDE Run action, reinstall, app-data clearing, or force-stop for this step. Those operations change the conditions you are trying to hold constant.Save the inputs needed to rebuild the screenThe fix is usually smaller than serializing the entire UI. A query or stable item ID can be enough to reconstruct data through the normal repository path.For state owned by a ViewModel, a minimal pattern is:import androidx.lifecycle.SavedStateHandleimport androidx.lifecycle.ViewModelimport kotlinx.coroutines.flow.StateFlowclass SearchViewModel( private val state: SavedStateHandle) : ViewModel() { val query: StateFlow = state.getStateFlow("query", "") fun setQuery(value: String) { state["query"] = value }}Connect the restored query to result loading and expose loading, empty, unavailable, and failure states explicitly. This fragment shows the state boundary; it is not a complete search implementation.SavedStateHandle’s documentation also defines an important boundary: saved state belongs to the task stack. Force-stopping the app, removing its task from Recents, or rebooting is not the same recovery promise. Use persistent storage for data that must outlive those events.There is a timing boundary too. The documentation warns that writes made while the host Activity is stopped are not saved unless it starts and stops again. Updating a saved-state value in background work is not an immediate durable commit.Keep useful smaller tests, with accurate labelsActivityScenario.recreate() destroys an Activity after saving its state and creates a replacement with the saved Bundle. Keep that test: it can find real recreation bugs. Name it for that behavior instead of calling it a process-death test.Likewise, Don’t keep activities destroys Activities when the user leaves them. Enabling it does not establish that the process and all its in-memory state disappeared. Use the process marker to check the difference.A ViewModel test constructed with a populated SavedStateHandle answers another narrow question: does this ViewModel interpret restored values correctly? It does not exercise Android capturing and delivering those values. Keep that distinction visible in the test report.Make failure classification part of the resultFor each run, record the build, device/API level, initial query and item ID, process markers, task-return path, and observed UI. Then distinguish three outcomes:Valid setup, correct reconstruction: the process changed, the task was resumed, and the screen met its contract.Valid setup, restoration failure: the intended interruption occurred, but required state was lost or resolved incorrectly.Invalid setup: the process did not change, the task was dismissed, or the fixture was not established.Repeat a valid process-death run with the data source unavailable. Preserving a query should not require pretending that fresh results have arrived. A recoverable error with the user’s input intact can satisfy the contract better than an indefinitely restored spinner.Finally, run task dismissal separately. A transient search can reset while an explicitly saved draft or record remains available. Whether that is correct comes from the product contract, not from the fact that one state API happens to retain a value.A restoration test earns its name when it proves both sides of the boundary: the old process is gone, and the new one rebuilt the intended experience.