Skip to content

Compose Navigator Tutorials

Kaustubh Patange edited this page Jun 15, 2021 · 20 revisions

These are small tutorials that covers some specific implementation detail.

Table of contents

Navigating with arguments

When you declare a Route say a sealed class (shown in Quick setup), the constructor parameters of the individual destination becomes the argument for that destination.

// Go to Quick setup guide to see the full example.
navigator.Setup(...) { controller, dest ->
    when(dest) {
        is MainRoute.First -> FirstScreen(dest.data, onChanged)
    }
}

Kotlin's is keyword will smartly cast the dest to the type we have specified so that we can easily access the arguments.

Navigating with animation

Navigator provides some custom enter (FadeIn, SlideInRight, SlideInLeft, ShrinkIn) & exit (FadeOut, SlideOutRight, SlideOutLeft, ShrinkOut) animation.

controller.navigateTo(dest) {
    withAnimation {
        enter = EnterAnimation.FadeIn
        exit = ExitAnimation.FadeOut
    }
}

On back press/back navigation these enter, exit animation will be played in reversed order. These information is also preserved accross configuration & process death.

Navigate with single top instance or popUpTo

controller.navigateTo(dest) {
    singleTop = true // makes sure that there is only one instance of this destination in backstack.
}
controller.navigateTo(dest) {
    popUpTo(dest) { // recursivly pops the backstack till the destination.
        inclusive = true // inclusive
        all = false // last added one will be chosen.
    }
}
Clone this wiki locally