Is there some updated docs and best practices arou...
# compose
t
Is there some updated docs and best practices around focus restoration with transitions ? Even if I can save and restore the focus in a focus group, during transition the focus is still loose and moved to the first top left item that is focusable.
Copy code
val fallbackRequester = remember { FocusRequester() }
    AnimatedContent(
        targetState = navigator.lastItem,
        transitionSpec = transition,
        modifier = modifier
            .focusGroup()
            .focusRestorer(fallback = fallbackRequester)
            .focusRequester(fallbackRequester),
        label = "",
    ) { screen ->
        navigator.saveableState("transition", screen) {
            content(screen)
        }
    }
This will properly save the focus and everything when leaving and returning to the group but the focus still leave the group during transition and so the wanted effect is lost, focus goes to the navigation rail instead of being kept in the current screen.
So one workaround is a full
Copy code
val fallbackRequester = remember { FocusRequester() }
    AnimatedContent(
        targetState = navigator.lastItem,
        transitionSpec = transitionSpec,
        modifier = modifier,
        label = "",
    ) { screen ->
        val transition = this.transition
        val isTransitioning = transition.currentState != transition.targetState

        LaunchedEffect(isTransitioning) {
            if (isTransitioning) {
                fallbackRequester.requestFocus()
                fallbackRequester.captureFocus()
            } else {
                fallbackRequester.freeFocus()
            }
        }

        Box(
            Modifier
                .focusGroup()
                .focusRestorer(fallback = fallbackRequester)
                .focusRequester(fallbackRequester),
        ) {
            navigator.saveableState("transition", screen) {
                content(screen)
            }
        }
    }
But then then freeFocus does not put back the focus to the saved one 😞
@Doris Liu Is this something specific to AnimatedContent is there anything better I can do here to actually have the proper focus restore and avoid focus leaving the transition content?