ui: restore search focus when returning

Keep the query populated and put the cursor back at the end of the search field when the screen is restored.
This commit is contained in:
austinried
2026-10-19 13:06:27 +00:00
parent 4b3a7d1dfa
commit 2e54d09acb
2 changed files with 3 additions and 11 deletions
@@ -23,7 +23,6 @@ import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember import androidx.compose.runtime.remember
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
@@ -56,13 +55,7 @@ fun SearchField(
textFieldValue = TextFieldValue(value, selection = TextRange(value.length)) textFieldValue = TextFieldValue(value, selection = TextRange(value.length))
} }
} }
var focusRequested by rememberSaveable { mutableStateOf(false) } LaunchedEffect(Unit) { focusRequester.requestFocus() }
LaunchedEffect(Unit) {
if (!focusRequested) {
focusRequester.requestFocus()
focusRequested = true
}
}
Surface( Surface(
shape = RoundedCornerShape(8.dp), shape = RoundedCornerShape(8.dp),
color = MaterialTheme.colorScheme.surfaceContainerHigh, color = MaterialTheme.colorScheme.surfaceContainerHigh,
@@ -7,7 +7,6 @@ import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue import androidx.compose.runtime.setValue
import androidx.compose.ui.test.assertIsDisplayed import androidx.compose.ui.test.assertIsDisplayed
import androidx.compose.ui.test.assertIsFocused import androidx.compose.ui.test.assertIsFocused
import androidx.compose.ui.test.assertIsNotFocused
import androidx.compose.ui.test.hasSetTextAction import androidx.compose.ui.test.hasSetTextAction
import androidx.compose.ui.test.junit4.StateRestorationTester import androidx.compose.ui.test.junit4.StateRestorationTester
import androidx.compose.ui.test.junit4.v2.createAndroidComposeRule import androidx.compose.ui.test.junit4.v2.createAndroidComposeRule
@@ -124,7 +123,7 @@ class SearchScreenTest {
} }
@Test @Test
fun restoringTheScreenDoesNotStealFocus() { fun restoringTheScreenKeepsFocusOnTheQuery() {
val restorationTester = StateRestorationTester(composeRule) val restorationTester = StateRestorationTester(composeRule)
restorationTester.setContent { restorationTester.setContent {
SubtracksTheme { SubtracksTheme {
@@ -135,7 +134,7 @@ class SearchScreenTest {
composeRule.onNode(hasSetTextAction()).assertIsFocused() composeRule.onNode(hasSetTextAction()).assertIsFocused()
restorationTester.emulateSavedInstanceStateRestore() restorationTester.emulateSavedInstanceStateRestore()
composeRule.onNode(hasSetTextAction()).assertIsNotFocused() composeRule.onNode(hasSetTextAction()).assertIsFocused()
} }
@Test @Test