You can checkout this sample implementation(working proper for me)
First, define transitions like below:
private const val TIME_DURATION = 300
val enterTransition: AnimatedContentTransitionScope<NavBackStackEntry>.() -> EnterTransition = {
slideInHorizontally(
initialOffsetX = { it },
animationSpec = tween(durationMillis = TIME_DURATION, easing = LinearOutSlowInEasing)
)
}
val exitTransition: AnimatedContentTransitionScope<NavBackStackEntry>.() -> ExitTransition = {
slideOutHorizontally(
targetOffsetX = { -it / 3 },
animationSpec = tween(durationMillis = TIME_DURATION, easing = LinearOutSlowInEasing)
)
}
val popEnterTransition: AnimatedContentTransitionScope<NavBackStackEntry>.() -> EnterTransition = {
slideInHorizontally(
initialOffsetX = { -it / 3 },
animationSpec = tween(durationMillis = TIME_DURATION, easing = LinearOutSlowInEasing)
)
}
val popExitTransition: AnimatedContentTransitionScope<NavBackStackEntry>.() -> ExitTransition = {
slideOutHorizontally(
targetOffsetX = { it },
animationSpec = tween(durationMillis = TIME_DURATION, easing = LinearOutSlowInEasing)
)
}
@ExperimentalAnimationApi
fun NavGraphBuilder.slideComposable(
route: String,
arguments: List<NamedNavArgument> = emptyList(),
content:
@Composable()
(AnimatedContentScope.(NavBackStackEntry) -> Unit)
) {
composable(
route,
arguments = arguments,
enterTransition = enterTransition,
exitTransition = exitTransition,
popEnterTransition = popEnterTransition,
popExitTransition = popExitTransition,
content = content
)
}
And use slideComposable function instead of composable() inside NavGraph:
NavHost(
navController,
startDestination = "destination",
modifier = Modifier) {
navigation(startDestination = "startDestination", route = "route") {
slideComposable("route) {
/*Composable()*/
}
}
}