https://kotlinlang.org logo
#compose
Title
# compose
c

chanjungskim

03/22/2023, 8:14 AM
How can I pass data from Ascreen in Agraph to Bscreen in Bgragh in compose navigation???
Here's my code:
Copy code
const val vitaminGraphRoutePattern = "vitamin_graph"
const val vitaminRoute = "vitamin_route"

fun NavController.navigateToVitamin(
    managerToken: String,
    managerId: Int,
    navOptions: NavOptions? = null
) {
    this.navigate(
        route = "$vitaminGraphRoutePattern?$MANAGER_TOKEN=$managerToken&$MANAGER_ID=$managerId",
        navOptions
    )
}

@OptIn(ExperimentalAnimationApi::class)
fun NavGraphBuilder.vitaminGraph(
    nestedGraphs: NavGraphBuilder.() -> Unit
) {
    navigation(
        route = "$vitaminGraphRoutePattern",
        startDestination = "$vitaminRoute"
    ) {
        nestedGraphs()
    }
}

@OptIn(ExperimentalAnimationApi::class)
fun NavGraphBuilder.vitaminScreen(
    moveToWelcomeSurvey: (surveyToken: String, answerData: QuestionAnswerData, managerToken: String, managerId: Int) -> Unit,
    moveToResult: (surveyToken: String, managerToken: String, managerId: Int) -> Unit
) {
    composable(
        route = "$vitaminRoute?" +
                "$MANAGER_TOKEN=${MANAGER_TOKEN}" +
                "&$MANAGER_ID=${MANAGER_ID}",
        arguments = listOf(
            navArgument(MANAGER_TOKEN) {
                type = NavType.StringType
                defaultValue = "no value"
            },
            navArgument(MANAGER_ID) {
                type = NavType.IntType
                defaultValue = -1
            }
        )
    ) { backStackEntry ->
        VitaminRoute(
            moveToWelcomeSurvey = { surveyToken, answerData ->
                moveToWelcomeSurvey(
                    surveyToken,
                    answerData,
                    backStackEntry.arguments?.getString(MANAGER_TOKEN) ?: "",
                    backStackEntry.arguments?.getInt(MANAGER_ID) ?: -1
                )
            },
            moveToResult = { surveyToken ->
                moveToResult(
                    surveyToken,
                    backStackEntry.arguments?.getString(MANAGER_TOKEN) ?: "",
                    backStackEntry.arguments?.getInt(MANAGER_ID) ?: -1
                )
            }
        )
    }
}
6 Views