Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple arguments with jetpack compose navigation

How do I declare a navigation route with multiple navigation arguments? I've checked the documentation, and all of these articles (which seem to simply reiterate what the documentation says), and I could only find examples of routes with one argument.

Here's what I have:

composable(
  route = "createExercise/{exerciseId}",
  arguments = listOf(navArgument("exerciseId") { type = NavType.IntType })
) { backStackEntry ->
  CreateExerciseScreen(
    exerciseId = backStackEntry.arguments!!.getInt("exerciseId"),
  )
}

Here's what I want:

composable(
  route = "createExercise/{exerciseId},{workoutId}",
  arguments = listOf(
    navArgument("exerciseId") { type = NavType.IntType },
    navArgument("workoutId") { type = NavType.IntType },
  )
) { backStackEntry ->
  CreateExerciseScreen(
    exerciseId = backStackEntry.arguments!!.getInt("exerciseId"),
    workoutId = backStackEntry.arguments!!.getInt("workoutId"),
  )
}

I arbitrarily chose a comma-seperated syntax for the example above in place of the real syntax which I am looking for.

So, my question is: When declaring a navigation route, what's the correct syntax for multiple arguments? (And what about optional arguments?)

like image 582
Noah Avatar asked Jan 02 '21 18:01

Noah


People also ask

How do I pass arguments to a route in jetpack compose?

The way it is currently done in jetpack compose is by appending the route with arguments and their respective values. For example, let’s say that we have a composable with route = “userPage”, and we want to pass arguments “userId” and “isLoggedIn”. The following snippets show how to do that in jetpack compose.

What is the navigation component used for?

The Navigation component provides support for Jetpack Compose applications. You can navigate between composables while taking advantage of the Navigation component’s infrastructure and features. Note: If you are not familiar with Compose, review the Jetpack Compose resources before continuing.

How do I pass arguments from one navigation route to another?

Navigation compose also supports passing arguments between composable destinations. In order to do this, you need to add argument placeholders to your route, similar to how you add arguments to a deep link when using the base navigation library: NavHost (startDestination = "profile/ {userId}") { ... composable ("profile/ {userId}") {...} }

How do I use compose with Android navcontroller?

To support Compose, use the following dependency in your app module’s build.gradle file: The NavController is the central API for the Navigation component. It is stateful and keeps track of the back stack of composables that make up the screens in your app and the state of each screen.


1 Answers

As per the docs:

You can think of it as an implicit deep link that leads to a specific destination.

So it follows the same conventions as any other implicit deep link and conventions of RESTful URLs on the web, which would generally use a / to separate different arguments to form the path of the URL - this covers the required arguments:

createExercise/{exerciseId}/{workoutId}

As per the optional arguments documentation that path of required arguments can be followed by any number of optional arguments in the form of one or more query parameters:

createExercise/{exerciseId}/{workoutId}?setNumber={setNumber}&repNumber={repNumber}
like image 193
ianhanniballake Avatar answered Sep 23 '22 06:09

ianhanniballake