1

I have created my NavGraph using the Kotlin DSL and everything is fine. But I'm struggling to pass a simple argument between destinations.

I'm folowing this Android Docs without success: https://developer.android.com/guide/navigation/navigation-kotlin-dsl#constants

Part of graph that adds the argument as the docs says:

            fragment<RestaurantsTabsFragment>(
                "${CampusSelectorDestinations.restaurantsTabsFragment}/" +
                        CampusSelectorArguments.campusId
            ) {
                argument(CampusSelectorArguments.campusId) {
                    type = NavType.StringType
                    defaultValue = "test"
                }
            }

Code with the navigation action trying to pass a argument:

        campusesAdapter.onCampusClick = { campusId ->
                 findNavController().navigate("${CampusSelectorDestinations.restaurantsTabsFragment}/" + campusId
        }

Error I get:

IllegalArgumentException: Navigation destination that matches request NavDeepLinkRequest{ uri=android-app://androidx.navigation/restaurantsTabsFragment/jCkuLbzRHtW0CUzDFWYw } cannot be found in the navigation graph NavGraph

Can anyone help me? I can provide more information if needed

2 Answers 2

2

The pattern to pass the argumet route is wrong at the docs:

enter image description here

For luck, I've found this explanation inside a Navigation Lib class and that solved my problem (after 2 days struggling):

... In addition to a direct Uri match, the following features are supported: Uris without a scheme are assumed as http and https. For example, www.example.com will match http://www.example.com and https://www.example.com. Placeholders in the form of {placeholder_name} matches 1 or more characters. The String value of the placeholder will be available in the arguments Bundle with a key of the same name. For example, http://www.example.com/users/{id} will match http://www.example.com/users/4. The .* wildcard can be used to match 0 or more characters. These Uris can be declared in your navigation XML files by adding one or more elements as a child to your destination. ...

Hope someone from Google see this and fixes the docs. (or explain if I'm wrong)

Sign up to request clarification or add additional context in comments.

3 Comments

You should file an issue to correct the documentation.
done Ian, thank you
For required args you do path/{name-of-argument}, for optional you do path?name={name-of-argument}. In the second case the "name" query paramenter can be anything, you just have to match it when navigating to this route. There is no option to have a route such as "user/{id}" and have the "id" being optional so it can match with both "/user" and "/suer/435". developer.android.com/jetpack/compose/navigation
1

Just put your arguments into curved breaks and separate them by slash as it shown in the example below.

  1. Define your destination with all required argument:

    fragment<TransactionFragment>("${MainNavRoute.transaction}/{arg1}/{arg2}") {
       argument("arg1") {
           type = NavType.StringType
       }
       argument("arg2") {
           type = NavType.LongType
       }
    
  2. Navigation to the destination:

    findNavController().navigate("${MainNavRoute.transaction}/string_value/2")

Also, I have reported an issue to the tracker too. https://issuetracker.google.com/issues/221895357

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.