New Compose Multiplatform components arrived on Composables UICheck it out →
Published on

How to navigate using Jetpack Compose Navigation with examples

RevenueCat logo


Composables UI

Become a sponsor

1 spots left!

This tutorial will cover how to implement Navigation in your Jetpack Compose application using the AndroidX Compose Navigation library.

Required dependency

Include the required dependency in your project's build.gradle file:

dependencies {
    def nav_version = "2.5.0"

    implementation "androidx.navigation:navigation-compose:$nav_version"

How to navigate between screens

Create a NavHost() and pass a NavController and the start route.

The NavController is used to perform navigation operations (such as navigate to the next screen or go back). Within the lambda function pass the available destinations (screens) of your app.

A basic app with a home screen and an about screen would look like this:

val navController = rememberNavController()
NavHost(navController, startDestination = "home") {
    composable("home") {
        HomeScreen(onAboutClick = {
    composable("about") {

How to pass data across destinations

When defining the route of your destination, pass in the argument(s) required as part of the route. The arguments need to be defined in a url like fashion:


named arguments are also supported:


pass the arguments as parameters in your destination. Each navArgument() requires a name and the expected type.

An example of a details screen requiring an id would look like this:

    arguments = listOf(navArgument("id") {
        type = NavType.LongType
    })) {
    val id = requireNotNull(it.arguments).getLong("id")

navigate to the destination using the NavController:

val id = 1000L

How to pass optional data across destinations

Any optional data needs to be defined as a query parameter. Include them in your route appending your required arguments with a ?:


additionally, mark the navArgument() as nullable:

    arguments = listOf(
        navArgument("id") {
            type = NavType.LongType
        navArgument("name") {
            type = NavType.StringType
            nullable = true
) { backStackEntry ->
    val arguments = requireNotNull(backStackEntry.arguments)
    val id = arguments.getLong("id")
    val name = arguments.getString("name")
    DetailsScreen(id, name)

navigate to the destination using the NavController:

val id = 1000L
val name = "Alex"

How to navigate to the previous screen

Use the navController.popBackStack() to remove the current destination from the back stack. This will cause the app to navigate to the previous destination, if there is one.

Keep in mind that the Navigation library follows the Principles of Navigation and will never terminate your application. If you wish to finish your activity if you are in the top-level destination of your app, use the return value of popBackStack() and call finish() if the value is true.

if(navController.popBackStack().not()) {

Checkout the Sample on Github

Check out the repository on Github and try it out on your machine:

  • Principles of Navigation: The Navigation library expects you to follow the Principles of Navigation while implementing. It is worth being aware of them so that you can understand its behavior and how to structure you graphs.
  • Compose Destinations: A library made by Rafael Costa that dramatically removes the boilerplate required for navigation.