4

I want to call this Compose as startDestination with arguments. I'm getting the below exception, but if i call this in another compose work as expected.

java.lang.IllegalArgumentException: navigation destination xxx is not a direct child of this NavGraph

implementation("androidx.navigation:navigation-compose:2.4.0-beta02")

SignInActivity:

class SignInActivity : BaseActivity() {

    @Inject
    lateinit var viewModelFactory: ViewModelFactory

    @ExperimentalComposeUiApi
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            val navController = rememberAnimatedNavController()
            ComposeTravelConnectTheme {
                val email="test@email"
                val route ="${Screens.VerifyRoute.route}/${OtpMode.EMAIL}?source=${email}"
                Timber.d("$route")
                Navigation(navController = navController, viewModelFactory,route)
            }
        }
    }
    //........
}

Navigation view:

@ExperimentalComposeUiApi
@ExperimentalAnimationApi
@Composable
fun Navigation(
    navController: NavHostController,
    factory: ViewModelFactory,
    startDestination: String = Screens.SignInOptionsScreensRoute.route
) {

    AnimatedNavHost(
        navController = navController,
        startDestination = startDestination
    ) {

        composable(
            route = "${Screens.VerifyRoute.route}/{otpMode}?source={source}",
            arguments = listOf(
                navArgument("otpMode") { type = NavType.StringType },
                navArgument("source") { type = NavType.StringType; defaultValue = "" },
            ),
            enterTransition = slideInEntryTransition,
            exitTransition = slideOutExitTransition,
            popEnterTransition = popOutEntryTransition,
        ) { backStackEntry ->

            val viewModel =
                viewModel(modelClass = OtpVerifyViewModel::class.java, factory = factory)

            viewModel.setOtpState(
                otpMode = OtpMode.fromStatusValue(backStackEntry.arguments?.getString("otpMode")),
                input = backStackEntry.arguments?.getString("source") ?: ""
            )
            OtpVerifyScreen(viewModel, navController)
        }
    }
}

Tried this SO answers it didn't help.

Phil Dukhov
  • 67,741
  • 15
  • 184
  • 220
Sam
  • 6,215
  • 9
  • 71
  • 90
  • It did not help me either –  Nov 30 '21 at 22:04
  • Did you solve the problem ? I get the same error while using navigation without Compose. – oiyio Dec 01 '21 at 13:45
  • 4
    [The answer](https://stackoverflow.com/questions/69038660/compose-navigation-navigation-destination-is-not-a-direct-child-of-this-na) you're linking at seems clear to me: your start destination cannot have arguments, it can only be exactly this string: `"${Screens.VerifyRoute.route}/{otpMode}?source={source}"`, in this case values passed for `defaultValue` will be used – Phil Dukhov Dec 07 '21 at 04:00
  • 1
    I ran into the same issue, and unfortunately Philip is right. I created a "routeFormat()" method to return the same messy string I feed into NavGraph composable method as the route and it finally worked as a startDestination. Why the startDestination route value is restricted in this way is beyond me. Hope your defaultValue's are acceptable, otherwise my guess is you'd be back to no solution. – jschlepp Dec 10 '21 at 23:21

0 Answers0