kotlin 如何在Jetpack合成中使用新参数向后导航?

xpcnnkqh  于 2023-01-31  发布在  Kotlin
关注(0)|答案(1)|浏览(130)

我想使用不同的输入参数返回。
我有一个ParentScreen,它正在使用输入参数arg(= "first")。代码通过在按下按钮时发送newArg(= "firstsecond")从父级导航到子级。当从子级导航回父级时,我希望父级的输入arg"fistsecond"
下面我写的代码(它是不工作),以显示我想要的.

@Composable
fun ParentScreen(
    nav: NavHostController,
    arg: String = "first"
) {
    val newArg = arg + "second"
    
    Button(onClick = { 
        nav.navigate("child/$newArg") {
            popUpTo("parent/$newArg") {
                saveState = true
            }
        }
    }) {
        Text(text = arg)
    }
}
nfeuvbwi

nfeuvbwi1#

您可能需要做的是将父级可组合对象作为single top启动,然后在从子级可组合对象导航时,可以发送新参数并再次将其作为single top启动。
您可以像这样第一次启动ParentScreen;

navController.navigate("parent/first") {
    launchSingleTop = true
}

然后像往常一样导航到ChildScreen;

navController.navigate("child/firstSecond")

在ChildScreen中,像这样导航到ParentSceen,这将从导航堆栈中删除ChildScreen,并将ParentScreen作为单顶启动,从而使堆栈中仅ParentScreen具有新参数;

navController.navigate("parent/$newArg") {
    launchSingleTop = true
    popUpTo("child/{someArg}") {
        inclusive = true
    }
}

完整的代码在这里;

@Composable
fun SampleNavHost(
    modifier: Modifier = Modifier,
    navController: NavHostController,
    onBackClick: () -> Unit,
    startDestination: String = "start_dest",
) {
    NavHost(
        modifier = modifier,
        navController = navController,
        startDestination = startDestination
    ) {
        composable("start_dest") {
            Button(onClick = {
                navController.navigate("parent/first") {
                    launchSingleTop = true
                }
            }) {
                Text(text = "move to parent")
            }
        }
        composable("parent/{someArg}") {
            val arg = it.arguments?.getString("someArg").orEmpty()
            ParentScreen(
                title = arg,
                navStackSize = navController.backQueue.size.toString(),
                onButtonClick = {
                    navController.navigate("child/firstSecond")
                }
            )
        }
        composable("child/{someArg}") {
            val arg = it.arguments?.getString("someArg").orEmpty()
            val newArg = arg + "second"
            ChildScreen(
                title = arg,
                onButtonClick = {
                    navController.navigate("parent/$newArg") {
                        launchSingleTop = true
                        // this will navigate current composable from stack when navigating
                        popUpTo("child/{someArg}") {
                            inclusive = true
                        }
                    }
                }
            )
        }
    }
}

@Composable
fun ParentScreen(
    title: String,
    navStackSize: String,
    onButtonClick: () -> Unit,
) {
    Column {
        Text(text = "Parent Screen, stack size: $navStackSize")
        Button(onClick = onButtonClick) {
            Text(text = title)
        }
    }
}

@Composable
fun ChildScreen(
    title: String,
    onButtonClick: (String) -> Unit,
) {
    Column {
        Text(text = "Child Screen")
        Button(onClick = { onButtonClick(title) }) {
            Text(text = title)
        }
    }
}

相关问题