From 2e54d09acb4985f7e5a2d7eaf51753b7dd1b1155 Mon Sep 17 00:00:00 2001 From: austinried <4966622+austinried@users.noreply.github.com> Date: Mon, 19 Oct 2026 13:06:27 +0000 Subject: [PATCH] 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. --- .../kotlin/com/subtracks/ui/components/SearchField.kt | 9 +-------- .../kotlin/com/subtracks/ui/search/SearchScreenTest.kt | 5 ++--- 2 files changed, 3 insertions(+), 11 deletions(-) diff --git a/app/src/main/kotlin/com/subtracks/ui/components/SearchField.kt b/app/src/main/kotlin/com/subtracks/ui/components/SearchField.kt index 153f2482..a64fc5f1 100644 --- a/app/src/main/kotlin/com/subtracks/ui/components/SearchField.kt +++ b/app/src/main/kotlin/com/subtracks/ui/components/SearchField.kt @@ -23,7 +23,6 @@ import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember -import androidx.compose.runtime.saveable.rememberSaveable import androidx.compose.runtime.setValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier @@ -56,13 +55,7 @@ fun SearchField( textFieldValue = TextFieldValue(value, selection = TextRange(value.length)) } } - var focusRequested by rememberSaveable { mutableStateOf(false) } - LaunchedEffect(Unit) { - if (!focusRequested) { - focusRequester.requestFocus() - focusRequested = true - } - } + LaunchedEffect(Unit) { focusRequester.requestFocus() } Surface( shape = RoundedCornerShape(8.dp), color = MaterialTheme.colorScheme.surfaceContainerHigh, diff --git a/app/src/test/kotlin/com/subtracks/ui/search/SearchScreenTest.kt b/app/src/test/kotlin/com/subtracks/ui/search/SearchScreenTest.kt index 3a096643..1ab07aef 100644 --- a/app/src/test/kotlin/com/subtracks/ui/search/SearchScreenTest.kt +++ b/app/src/test/kotlin/com/subtracks/ui/search/SearchScreenTest.kt @@ -7,7 +7,6 @@ import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.setValue import androidx.compose.ui.test.assertIsDisplayed import androidx.compose.ui.test.assertIsFocused -import androidx.compose.ui.test.assertIsNotFocused import androidx.compose.ui.test.hasSetTextAction import androidx.compose.ui.test.junit4.StateRestorationTester import androidx.compose.ui.test.junit4.v2.createAndroidComposeRule @@ -124,7 +123,7 @@ class SearchScreenTest { } @Test - fun restoringTheScreenDoesNotStealFocus() { + fun restoringTheScreenKeepsFocusOnTheQuery() { val restorationTester = StateRestorationTester(composeRule) restorationTester.setContent { SubtracksTheme { @@ -135,7 +134,7 @@ class SearchScreenTest { composeRule.onNode(hasSetTextAction()).assertIsFocused() restorationTester.emulateSavedInstanceStateRestore() - composeRule.onNode(hasSetTextAction()).assertIsNotFocused() + composeRule.onNode(hasSetTextAction()).assertIsFocused() } @Test