Jetpack Navigation Compose Animation¶
A library which provides Compose Animation support for Jetpack Navigation Compose.
Warning
This library is deprecated, with official navigation-compose support in androidx.navigation.compose. The original documentation is below the migration guide.
Migration¶
The official androidx.navigation.compose version 2.7.0-alpha01+ offers all of the same functionality as Accompanist Navigation Animation.
- Make sure you are using Compose 1.5.0-beta01+ before migrating to androidx.navigation.compose.
- Replace dependency com.google.accompanist:accompanist-navigation-animation:<version>withandroidx.navigation:navigation-compose:<version>
- Replace rememberAnimatedNavControllerwithrememberNavControllerand change import toandroidx.navigation.compose.rememberNavController
- Replace AnimatedNavHostwithNavHostand change import toandroidx.navigation.compose.NavHost
- Replace AnimatedComposeNavigatorwithComposeNavigatorand change import toandroidx.navigation.compose.ComposeNavigator
- Replace AnimatedComposeNavigator()constructor withComposeNavigator()constructor
- Replace AnimatedComposeNavigator.DestinationwithComposeNavigator.Destination
- Change import for composable from com.google.accompanist.navigation.animation.composabletoandroidx.navigation.compose.composable
- Change import for navigation from com.google.accompanist.navigation.animation.navigationtoandroidx.navigation.compose.navigation
Migration Table¶
The following is a mapping of Navigation classes and functions from accompanist to androidx.compose:
| accompanist navigation-animation | androidx.navigation.compose | 
|---|---|
| AnimatedNavHost | NavHost | 
| AnimatedComposeNavigator | ComposeNavigator | 
| AnimatedComposeNavigator.Destination | ComposeNavigator.Destination | 
| rememberAnimatedNavController() | rememberNavController() | 
| NavGraphBuilder.composable() | NavGraphBuilder.composable() | 
| NavGraphBuilder.navigation() | NavGraphBuilder.navigation() | 
Of note, ComposeNavigation.Destination allows use of AnimatedContentScope instead of just AnimatedVisibilityScope.
Deprecated Guidance for Accompanist Navigation¶
The following is the deprecated guide for using Navigation in Accompanist. Please see above migration section for how to use the androidx.navigation.compose Navigation.
Download¶
repositories {
    mavenCentral()
}
dependencies {
    implementation "com.google.accompanist:accompanist-navigation-animation:<version>"
}
Follow the steps below to either add Jetpack Navigation Compose to your app, or to migrate an existing Jetpack Navigation Compose implementation.
Usage¶
The AnimatedNavHost composable offers a way to add custom transitions to composables in
Navigation Compose via parameters that can be attached to either an individual composable
destination, a navigation element, or to the AnimatedNavHost itself.
Each lambda has an AnimatedContentScope<NavBackStackEntry> receiver scope that allows you to use special transitions (such as slideIntoContainer and slideOutOfContainer) and gives you access to the initialState and targetState properties that let you customize what transitions are run based on what screen you are transitioning from (the initialState) and transitioning to (the targetState). 
- enterTransitioncontrols what- EnterTransitionis run when the- targetState- NavBackStackEntryis appearing on the screen.
- exitTransitioncontrols what- ExitTransitionis run when the- initialState- NavBackStackEntryis disappearing from the screen.
- popEnterTransitiondefaults to- enterTransition, but can be overridden to provide a separate- EnterTransitionwhen the- targetState- NavBackStackEntryis appearing on the screen due to a pop operation (i.e.,- popBackStack()).
- popExitTransitiondefaults to- exitTransition, but can be overridden to provide a separate- ExitTransitionwhen the- initialState- NavBackStackEntryis disappearing from the screen due to a pop operation (i.e.,- popBackStack()).
For each transition, if a composable destination returns null, the parent navigation element's transition will be used, thus allowing you to set a global set of transitions at the navigation graph level that will apply to every composable in that graph. This continues up the hierarchy until you reach the root AnimatedNavHost, which controls the global transitions for all destinations and nested graphs that do not specify one.
Note: this means that if a destination wants to instantly jump cut between destinations, it should return EnterTransition.None or ExitTransition.None to signify that no transition should be run, rather than return null.
@Composable
private fun ExperimentalAnimationNav() {
    val navController = rememberAnimatedNavController()
    AnimatedNavHost(navController, startDestination = "Blue") {
        composable(
            "Blue",
            enterTransition = {
                when (initialState.destination.route) {
                    "Red" ->
                        slideIntoContainer(AnimatedContentScope.SlideDirection.Left, animationSpec = tween(700))
                    else -> null
                }
            },
            exitTransition = {
                when (targetState.destination.route) {
                    "Red" ->
                        slideOutOfContainer(AnimatedContentScope.SlideDirection.Left, animationSpec = tween(700))
                     else -> null
                }
            },
            popEnterTransition = {
                            when (initialState.destination.route) {
                                "Red" ->
                                    slideIntoContainer(AnimatedContentScope.SlideDirection.Right, animationSpec = tween(700))
                                else -> null
                            }
                        },
            popExitTransition = {
                when (targetState.destination.route) {
                    "Red" ->
                        slideOutOfContainer(AnimatedContentScope.SlideDirection.Right, animationSpec = tween(700))
                    else -> null
                }
            }
        ) { BlueScreen(navController) }
        composable(
            "Red",
            enterTransition = {
                when (initialState.destination.route) {
                    "Blue" ->
                        slideIntoContainer(AnimatedContentScope.SlideDirection.Left, animationSpec = tween(700))
                    else -> null
                }
            },
            exitTransition = {
                when (targetState.destination.route) {
                    "Blue" ->
                        slideOutOfContainer(AnimatedContentScope.SlideDirection.Left, animationSpec = tween(700))
                    else -> null
                }
            },
            popEnterTransition = {
                when (initialState.destination.route) {
                    "Blue" ->
                        slideIntoContainer(AnimatedContentScope.SlideDirection.Right, animationSpec = tween(700))
                    else -> null
                }
            },
            popExitTransition = {
                when (targetState.destination.route) {
                    "Blue" ->
                        slideOutOfContainer(AnimatedContentScope.SlideDirection.Right, animationSpec = tween(700))
                    else -> null
                }
            }
        ) { RedScreen(navController) }
    }
}
For more examples, refer to the samples.
Migration¶
To migrate from using the Navigation Compose APIs do the following:
- Replace rememberNavController()withrememberAnimatedNavController()
- Replace NavHostwithAnimatedNavHost
- Replace import androidx.navigation.compose.navigationwithimport com.google.accompanist.navigation.animation.navigation
- Replace import androidx.navigation.compose.composablewithimport com.google.accompanist.navigation.animation.composable
Snapshots of the development version are available in Sonatype's snapshots repository. These are updated on every commit.
For more details see Animations in Navigation Compose