How can I change the navigation animation of the same destination when navigating in jetpack compose? For example, I want to show the screen horizontally from the right and other times I want that same screen to come vertically from the bottom (when clicking on a notification). I can't really do it using navController.navigate(route) because the route is defined on the same hierarchy as the enterTransition and exitTransition.
composable(
route = "Details",
deepLinks = listOf(navDeepLink { uriPattern = "myapp://test/details" }),
enterTransition = {
slideInVertically(
initialOffsetY = { it },
animationSpec = tween(durationMillis = 700, easing = LinearOutSlowInEasing)
)
}
},
exitTransition = {
ExitTransition.None
},
popEnterTransition = {
EnterTransition.None
},
popExitTransition = {
slideOutVertically(
targetOffsetY = { it },
animationSpec = tween(durationMillis = 700, easing = LinearOutSlowInEasing)
)
},
) {
MyComposable()
}
You can utilize navigation arguments. Once you have set up the navigation arguments, you can access the argument value and adjust the transition behavior. Here's an example:
NavHost(..) {
composable(
route = "Details/{MyAnimation}",
enterTransition = {
val slideDirection = this.targetState.arguments?.getString("MyAnimation") ?: "start"
val slideDirectionEnum = when (slideDirection) {
"slideUp" -> AnimatedContentTransitionScope.SlideDirection.up
"slideDown" -> AnimatedContentTransitionScope.SlideDirection.down
else -> AnimatedContentTransitionScope.SlideDirection.start // default
}
fadeOut(
animationSpec = tween(
durationMillis = 300,
easing = LinearEasing
)
) + slideOutOfContainer(
animationSpec = tween(durationMillis = 300, easing = EaseOut),
towards = slideDirection
)
},
){
MyComposable()
}
composable("OtherComposable"){
OtherComposableScreen(
onNavigateToDetails = { navController.navigate("Details/slideUp") }
)
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With