Jetpack compose allows developers to seamlessly integrate impressive animations between screens to enrich the use experience. This results in a reduction of code and enhances its readability. The feature of Jetpack Compose simplifies the complexity of animations in mobile app development, saving developers time.

navigation animated


Before we start do not forget to implement the below dependency:

implementation "androidx.navigation:navigation-compose:2.7.0"


And then let’s prepare example screens:

@Composable
fun ScreenA(navController: NavController) {
    Box(
        modifier = Modifier
            .fillMaxSize(),
       contentAlignment = Alignment.Center
    ) {

       Text(
           modifier = Modifier
               .padding(top=15.dp)
               .align(Alignment.TopCenter),
           text = "Screen A",
           fontSize = 25.sp,
           fontWeight = FontWeight.Bold,
       )

       Button(
           onClick = {
               navController.navigate("SCREEN_B")
           }

       ) {
           Text(text = "Screen B")
       }
    }
}

@Composable
fun ScreenB() {
    Box(
        modifier = Modifier
            .fillMaxSize(),
        contentAlignment = Alignment.Center
    ) {
        Text(
            text = "Screen B",
            fontSize = 25.sp,
            fontWeight = FontWeight.Bold
        )
    }
}


Set up your navigation host like the below.

                NavHost(
                    navController = navHostController,
                    startDestination = "SCREEN_A"
                ) {
                    composable(
                        route = "SCREEN_A"
                    ) {
                        ScreenA(navController = navHostController)
                    }

                    composable(
                        route = "SCREEN_B",
                        enterTransition = {
                            slideIntoContainer(
                                towards = AnimatedContentTransitionScope.SlideDirection.Companion.Left,
                                animationSpec = tween(700)
                            )
                        },

                        exitTransition = {
                            slideOutOfContainer(
                                towards = AnimatedContentTransitionScope.SlideDirection.Companion.Left,
                                animationSpec = tween(700)
                            )
                        },
                        popEnterTransition = {
                            slideIntoContainer(
                                towards = AnimatedContentTransitionScope.SlideDirection.Companion.Right,
                                animationSpec = tween(700)
                            )
                        },
                        popExitTransition = {
                            slideOutOfContainer(
                                towards = AnimatedContentTransitionScope.SlideDirection.Companion.Right,
                                animationSpec = tween(700)
                            )
                        }
                    ) {
                        ScreenB()
                    }
                }


The properties enterTransition, exitTransition, popEnterTransition, and popExitTransition are utilized to define animations during screen transitions. Let’s delve into these properties in detail:

enterTransition: This defines the animation to be used when a screen is being displayed. For instance, when transitioning to the ScreenB, this animation is executed.

  • slideIntoContainer: Allows the screen to slide in from the outside.
  • towards = AnimatedContentTransitionScope.SlideDirection.Companion.Left: Indicates that the screen slides in from the left.
  • animationSpec = tween(700): Specifies that the animation will last for 700 milliseconds.


exitTransition:
This defines the animation to be used when a screen is being overlaid by another screen. For example, when transitioning away from the ScreenB to another screen, this animation is executed.

  • slideOutOfContainer: Allows the screen to slide out to the outside.
  • towards = AnimatedContentTransitionScope.SlideDirection.Companion.Left: Indicates that the screen slides out to the left.


popEnterTransition:
This defines the animation to be used when the back button is pressed or when navigating backward using the NavController.

  • slideIntoContainer: Allows the screen to slide in from the outside.
  • towards = AnimatedContentTransitionScope.SlideDirection.Companion.Right: Indicates that the screen slides in from the right.


popExitTransition:  This defines the animation for how the previous screen will disappear when the back button is pressed or when navigating backward using the NavController.

  • slideOutOfContainer: Allows the screen to slide out to the outside.
  • towards = AnimatedContentTransitionScope.SlideDirection.Companion.Right: Indicates that the screen slides out to the right.

Through these animations, transitions between screens are made smoother and more aesthetically pleasing. This code snippet facilitates sliding transitions between ScreenA and ScreenB screens.