title: Jetpack Compose Navigation - pass argument to startDestination
body: <p>The app I'm building uses compose navigation with routes. The challenge is that the start destination is dynamic.</p>
  <p>Here is a minimal example:</p>
  <pre><code>class MainActivity : ComponentActivity()
  {
      override fun onCreate(savedInstanceState: Bundle?)
      {
          super.onCreate(savedInstanceState)
  
          setContent {
              val navController = rememberNavController()
  
              NavHost(
                  navController = navController,
                  startDestination = &quot;dynamic/1&quot;, // doesn't work
                  // startDestination = &quot;static&quot;, // workaround
              ) {
                  composable(
                      route = &quot;dynamic/{$ARG_ID}&quot;,
                      arguments = listOf(navArgument(ARG_ID) { type = NavType.StringType }),
                  ) {
                      val id = it.arguments?.getString(ARG_ID)
                      Text(&quot;dynamic route, received argument: $id!&quot;)
                  }
                  // part of the workaround
                  // composable(
                  //  route = &quot;static&quot;,
                  // ) {
                  //  LaunchedEffect(this) {
                  //      navController.navigate(&quot;dynamic/1&quot;)
                  //  }
                  // }
              }
          }
      }
  
      companion object
      {
          const val ARG_ID = &quot;id&quot;
      }
  }
  </code></pre>
  <p>The app crashes with</p>
  <pre><code>java.lang.IllegalArgumentException: navigation destination route/1 is not a direct child of this NavGraph
  </code></pre>
  <p>The problem only exists if the &quot;dynamic&quot; route is used as start destination. This can be verified by using <code>startDestination = &quot;static&quot;</code>.</p>
  <p>Although, the &quot;static&quot; route workaround works I'm looking for a solution without it because it kind of obfuscates the code and also creates an additional entry in the back stack.</p>
  <p>-&gt; <a href="https://github.com/p-fischer/compose-navigation-startdestination-with-argument" rel="noreferrer">Full code sample</a> to reproduce the issue</p>
  <p>Related SO questions</p>
  <ul>
  <li><a href="https://stackoverflow.com/q/50334550/2011622">Navigation Architecture Component- Passing argument data to the startDestination</a> - Answers don't seem to be applicable to Compose Navigation.</li>
  <li><a href="https://stackoverflow.com/q/70086094/2011622">Pass an argument to a nested navigation graph in Jetpack Compose</a> - No answer given.</li>
  <li><a href="https://stackoverflow.com/q/69038660/2011622">Compose Navigation - navigation destination ... is not a direct child of this NavGraph</a> - The accepted answer doesn't resolve the issue.</li>
  </ul>
  <p><strong>Edit:</strong></p>
  <p>I want to stress that the original sample used to not contain the &quot;static&quot; composable. I only added the &quot;static&quot; composable to have a working <code>startDestination</code> and to prove that the &quot;dynamic&quot; composable can be navigated to.</p>
  <p><strong>Update:</strong></p>
  <p>Even switching to the <a href="https://developer.android.com/jetpack/compose/navigation#optional-args" rel="noreferrer">query parameter syntax for optional arguments</a>, providing a default value, and setting the start destination without any argument does not work.</p>
  <p>The following variation</p>
  <pre><code>NavHost(
      navController = navController,
      startDestination = &quot;dynamic&quot;,
  ) {
      composable(
          route = &quot;dynamic?$ARG_ID={$ARG_ID}&quot;,
          arguments = listOf(navArgument(ARG_ID) { type = NavType.StringType; defaultValue = &quot;1&quot; }),
      ) {
          val id = it.arguments?.getString(ARG_ID)
          Text(&quot;dynamic route, received argument: $id!&quot;)
      }
  }
  </code></pre>
  <p>Leads to the exception</p>
  <pre><code>java.lang.IllegalArgumentException: navigation destination dynamic is not a direct child of this NavGraph
  </code></pre>