Alex Zaitsev
02/07/2024, 9:18 PM.focusRestorer()
modifier. It's in experimental state unfortunately. Maybe someone knows a workaround?
val focusRequester = remember { FocusRequester() }
val focusModifier = Modifier.focusRequester(focusRequester)
LazyColumn(
modifier = Modifier.focusRestorer { focusRequester },
) {
// number of items is more the screen is hold, so some of them are not visible
items(state.dynamicNavMenuItems) { dynamicNavMenuItem ->
DynamicNavElementItem(
modifier = if (dynamicNavMenuItem.initiallyFocused) focusModifier else Modifier,
)
}
// focusModifier is assigned to last focused item that is stored as a part of state
}
the issue appears when desired item to focus is out of the screen.
usecase:
1. user selected item
2. another screen is shown
3. user is returned to the screen -> focusRestorer tries to restore
4. desired view is out of visibility
5. as restorer is not so clever to scroll it fails to restore -> provided focusRequester
is used
6. app crashes with FocusRequesterNotInitialized
I tried to combine it somehow with BringIntoViewRequester()
but no luck so far as focusRestorer
doesn't wait for any other code. It tries to restore straight away when the screen is visible and doesn't wait for anybody.
ideas?Alex Zaitsev
02/07/2024, 9:18 PMZach Klippenstein (he/him) [MOD]
02/07/2024, 9:21 PM