Hi everyone, I have a question about navigation an...
# compose
g
Hi everyone, I have a question about navigation and route animations. I am creating a circular carousel that has several pages in it. Due to how the navcontroller works every time I navigate to a new route in the carousel (either forward or back) it pushes that route onto the backstack and shows the forward animation. When going back through the carousel I would like to show the animation as if the page was popped from the backstack, but since the page is actually being pushed onto the backstack it shows the forward animation. I tried the code below to show the animation as if it popped off the backstack but it does not work. Does anyone know how I can show the pop backstack animation while actually pushing an item onto the backstack? Or, if you have a different idea about how to implement the carousel so the correct forward and backward animations are shown I am open to trying a different strategy. But the goal is that even from the very first page of the carousel it should be able to navigate to the last page by pressing the back arrow I have created, giving the user a circular carousel experience.
Copy code
private fun generateNavigatorLambda(
    navController: NavController,
    viewModel: SlideshowViewModel<*>
): () -> Unit = {

    val value = viewModel.state.value
    value.navigateTo?.let { route ->
        if(route == Route.Start) {
            navController.popBackStack(route, inclusive = false)
        } else {
            if (value.navigateBack)
                navController.navigate(route) {
                    slideOutHorizontally(
                        targetOffsetX = { fullWidth -> -fullWidth }, // Slide out to the right when this screen is popped
                        animationSpec = tween(durationMillis = 300)
                    ) + fadeOut(animationSpec = tween(300))
                }
            else
                navController.navigate(route)
        }
    }
}
i
I think if you were dead set on using Navigation, you should be writing your own NavHost that uses a
HorizontalPager
/
HorizontalUncontainedCarousel
But IMO, this doesn't feel like something you should be using Navigation for at all
Carousels generally don't interact with system back at all and even if you did want that part of Navigation, it would be much easier to connect that to your own UI via the
PredictiveBackHandler
yourself