GDefender
GDefender

Reputation: 551

Jetpack Compose Wear OS Scroll to top onResume

I have a Wear OS Overlay built with Compose, that uses a ScalingLazyColumn and rememberScalingLazyListState() for scrolling. When the user leaves the overlay and returns I want the column to scroll to the top instead of saving their location. Is there a way to do this?

The screen uses LiveData/State so elements recompose while the user is on the screen, and I do not want to lose their scroll position in this case.

@Composable
fun WearApp(weatherVM: WeatherViewModel, application: Application) {
    WearAppTheme {
        val weather = weatherVM.weather.observeAsState(initial = null)
        val listState = rememberScalingLazyListState()
        Scaffold(
            ...
            positionIndicator = {
                PositionIndicator(
                    scalingLazyListState = listState
                )
            }
        ) {
            ScalingLazyColumn(
                modifier = Modifier.fillMaxSize(),
                autoCentering = AutoCenteringParams(itemIndex = 0),
                state = listState
            ) {
                item {
                    ...
                }
                item {
                    ...
                }
                ...

Upvotes: 2

Views: 1008

Answers (1)

Yuri Schimke
Yuri Schimke

Reputation: 13488

If you don't have navigation, then you should just be able to use lifecycle events to change the list state on resume.

See https://stackoverflow.com/a/66807899/1542667

If you do use navigation, then you'll need to get the Lifecycle above the SwipeDismissableNavHost, as each navigation destination has it's own lifecycle, resume could be called when you swipe back to the top level.

            LaunchedEffect(Unit) {
                lifecycleOwner.repeatOnLifecycle(state = Lifecycle.State.RESUMED) {
                    scrollState.scrollToItem(index = 0)
                }
            }

Upvotes: 0

Related Questions