By Shakil Ahmed, Android developer & software engineer
Keep a Compose Search Query After Android Recreates the App
Use SavedStateHandle to restore a Compose search query, reload its results, and test the difference between rotation and process recreation.
Type “camera” into a product search, switch to another app, and come back later. Seeing an empty search box is frustrating. Seeing “camera” above an unfiltered list is worse: the field and the results disagree. The fix is to give the query one owner and derive the visible results from that value.
If you followed Add Search and Favorites with a ViewModel, you already have a place to hold screen state. A ViewModel typically survives rotation, but its in-memory properties disappear when Android kills the process. For a small value needed to reconstruct the screen, use saved state. Android's Compose state-saving guide explains the distinction.
Store the input, then rebuild the list
Here is the query-owning part of a Kotlin ViewModel. The example assumes the app uses the usual Android ViewModel creation mechanism that supplies a SavedStateHandle.
class SearchViewModel(private val saved: SavedStateHandle) : ViewModel() {
val query = saved.getStateFlow("search_query", "")
fun changeQuery(value: String) {
saved["search_query"] = value
}
}
In your composable, collect the flow with collectAsStateWithLifecycle(). Pass its current value to the text field and viewModel::changeQuery as the change callback. Use that same query to filter the current product list. Avoid a second, independently remembered string inside the text field.
Imagine the catalog changes while you're away. Restoring a serialized list of old matches would bring back stale prices along with the query. Restoring just “camera” lets the app calculate matches against today's catalog. If the catalog is still loading, show the restored query immediately and make the loading state explicit.
Test the failure you care about
First type a query and rotate the emulator. Check both the field and the results. Then send the app to the background, run adb shell am kill your.package.name on a development device, and reopen the existing task from Recents. This second check exercises process recreation when the background process is killed and the task is retained. Force-stopping the app or swiping the task away tests a different lifecycle.
Saved state is a convenience for restoring an interrupted task. If the requirement is “remember my preferred filter next week, even after a fresh launch,” use persistent storage instead. The DataStore preferences tutorial shows that design.
Related articles
Build a secure AI chat in your Android app with Firebase AI Logic and Gemini: setup, App Check, model choice, streaming ViewModel, Compose UI and error handling.
Create your first Android project, understand every line of MainActivity.kt in plain English, and run your Hello World app on an emulator or your phone.
Pick a photo, read its text on-device with ML Kit Text Recognition v2, and show blocks and lines in Jetpack Compose with a copy button.
Run AI directly on the phone with Gemini Nano and ML Kit GenAI APIs: privacy, offline use, device support, a Compose summarizer, cloud fallback and limits.
