代码之家  ›  专栏  ›  技术社区  ›  James Black

使用kotlin多平台从一个Android屏幕导航到另一个屏幕时出现深度链接异常

  •  0
  • James Black  · 技术社区  · 3 年前

    我在主活动中有这个:

    @Composable
    fun createNavHost(paddingValues: PaddingValues, navController: NavHostController) {
        NavHost(navController, startDestination = Screen.HomeScreen.title) {
            composable(Screen.HomeScreen.title) {
                HomeScreen(
                    accountSelected = {
                        navController.navigate(Screen.PasswordDetailScreen.title + "/${it.id}")
                    })
            }
            composable(Screen.EditPasswordScreen.title) {
                EditPasswordScreen()
            }
        }
    }
    

    我的布局是这样定义的:

    @Composable
    fun MainLayout() {
        val navController = rememberNavController()
    
        PasswordHelperTheme {
            Scaffold(
            ) { paddingValues ->
                createNavHost(paddingValues, navController)
            }
        }
    }
    

    屏幕定义如下:

    sealed class Screen(val title: String) {
        object HomeScreen : Screen("Home")
        object SettingsScreen : Screen("Settings")
        object EditPasswordScreen : Screen("Edit")
        object PasswordDetailScreen: Screen("ArticleDetail")
    }
    

    我打电话来

    @可组合式 有趣的AddAccountFAB(){ val上下文=本地上下文 val-navController=记住navController()

    ExtendedFloatingActionButton(
        shape = CircleShape,
        icon = { Icon(Icons.Filled.Add, "") },
        onClick = {
            navController.graph = navController.graph
            navController.navigate(MainActivity.Screen.EditPasswordScreen.title) {
                popUpTo = navController.graph.startDestination
                launchSingleTop = true
            }
        },
        elevation = FloatingActionButtonDefaults.elevation(8.dp),
        text = { Text("Add Account")}
    )
    

    并得到

    java.lang.IllegalStateException: You must call setGraph() before calling getGraph()
            at androidx.navigation.NavController.getGraph(NavController.java:812)
            at com.blackfox.passwordhelper.android.HomeScreenKt$AddAccountFAB$1$1.invoke(HomeScreen.kt:79)
    

    我想 createNavHost 正在设置图表。这至少让我通过了深度链接例外。我只是想用FAB转到一个页面。

    0 回复  |  直到 3 年前
        1
  •  0
  •   Brady    3 年前

    尝试删除

    navController.graph = navController.graph
    
        2
  •  0
  •   James Black    3 年前

    看来唯一真正的选择是建立一个 route navHost .

    NavHost(navController, startDestination = Screen.HomeScreen.title) {
            composable(Screen.HomeScreen.title) {
                HomeScreen(
                    accountSelected = {
                        navController.navigate(Screen.PasswordDetailScreen.title + "/${it.id}")
                    },
                    fabSelected = {
                        navController.navigate(Screen.EditPasswordScreen.title + "/${it.id}")
                    })
            }
            composable(Screen.EditPasswordScreen.title + "/-1") {
                EditPasswordScreen()
            }
        }
    

    此方法有效:

    ExtendedFloatingActionButton(
        shape = CircleShape,
        icon = { Icon(Icons.Filled.Add, "") },
        onClick = {
            fabSelected(Passwords(-1, "", "", "", "", ""))
        },
        elevation = FloatingActionButtonDefaults.elevation(8.dp),
        text = { Text("Add Account")}
    )