Jetpack Compose:深度链接后底栏导航无响应

问题描述 投票:0回答:2

我在我的新 Jetpack Compose 应用程序中设置了一个底栏,其中包含 2 个目的地。我试着按照谷歌的样本。

所以例如它看起来像这样:

@Composable
fun MyBottomBar(navController: NavHostController) {
    val items = listOf(
        BottomNavigationScreen.ScreenA,
        BottomNavigationScreen.ScreenB
    )
    val navBackStackEntry by navController.currentBackStackEntryAsState()
    val currentDestination = navBackStackEntry?.destination

    BottomNavigation {
        items.forEach { screen ->
            BottomNavigationItem(
                onClick = {
                    navController.navigate(screen.route) {
                        popUpTo(navController.graph.findStartDestination().id) {
                            saveState = true
                        }

                        launchSingleTop = true
                        restoreState = true

                    }
                },
                selected = currentDestination?.hierarchy?.any { it.route == screen.route } == true,
                icon = { Icon(imageVector = screen.icon, contentDescription = null) },
                label = { Text(stringResource(screen.label)) }
            )
        }
    }
}

一切正常,我可以在两个目的地之间导航。但是,我也有一个深层链接到

ScreenB
。一旦调用了它,按下
ScreenA
按钮似乎什么都不做(如果我添加日志记录,我可以看到 currentDestination 被重复设置为
ScreenB
)但是按下返回到 startDestination
ScreenA
.

我目前的解决方法是从示例代码中删除

restoreState = true
行。

我的怀疑是关于深层链接的某些东西被持久化了,虽然它试图去 ScreenA,但导航组件说它有一个指向 ScreenB 的深层链接,所以它只是去那里。我尝试过重置活动意图,使其在意图中没有标志和数据,我什至尝试过更改意图操作类型,但都无济于事。

我正在使用 Compose 1.0.0-rc02 和 Compose Navigation 2.4.0-alpha04.

我做错了什么还是这是一个错误?

deep-linking android-jetpack-compose android-architecture-navigation
2个回答
2
投票

我知道您从官方文档中获得了这段代码,但我认为它不适用于底部导航。它将元素保留在导航堆栈中,因此从

ScreenB
按下后退按钮将带您返回
ScreenA
,在我看来这在这种情况下不是正确的行为。

这就是为什么最好从堆栈中删除所有元素,以便始终只留下一个选项卡。使用

saveState
无论如何你都不会丢失状态。这可以按如下方式完成:

fun NavHostController.navigateBottomNavigationScreen(screen: BottomNavigationScreen) = navigate(screen.route) {
    val navigationRoutes = BottomNavigationScreen.values()
        .map(BottomNavigationScreen::route)
    val firstBottomBarDestination = backQueue
        .firstOrNull { navigationRoutes.contains(it.destination.route) }
        ?.destination
    if (firstBottomBarDestination != null) {
        popUpTo(firstBottomBarDestination.id) {
            inclusive = true
            saveState = true
        }
    }
    launchSingleTop = true
    restoreState = true
}

像这样使用它:

BottomNavigationItem(
    onClick = {
        navController.navigateBottomNavigationScreen(screen)
    },
    selected = currentDestination?.hierarchy?.any { it.route == screen.route } == true,
    icon = { Icon(imageVector = screen.icon, contentDescription = null) },
    label = { Text(screen.label) }
)

出于同样的原因,在这种情况下我不会使用深度链接导航。相反,您手动处理它们。如果您离开底部导航视图并返回,您可以使用视图模型不重新处理深层链接:

class DeepLinkProcessingViewModel : ViewModel() {
    private var deepLinkProcessed = false

    fun processDeepLinkIfAvailable(context: Context): String? {
        if (!deepLinkProcessed) {
            val activity = context.findActivity()
            val intentData = activity?.intent?.data?.toString()

            deepLinkProcessed = true
            return intentData
        }
        return null
    }
}

使用此视图模型,您可以这样计算起始目的地:

val context = LocalContext.current
val deepLinkProcessingViewModel = viewModel<DeepLinkProcessingViewModel>()
val startDestination = rememberSaveable(context) {
    val deepLink = deepLinkProcessingViewModel.processDeepLinkIfAvailable(context)
    if (deepLink == "example://playground") {
        // deep link handled
        BottomNavigationScreen.ScreenB.route
    } else {
        // default start destination
        BottomNavigationScreen.ScreenA.route
    }
}
NavHost(navController = navController, startDestination = startDestination) {
    ...
}

或者,如果你在底部导航前面有几个导航元素,你不想用深层链接丢失它们,你可以这样做:

val navController = rememberNavController()
val context = LocalContext.current
val deepLinkProcessingViewModel = viewModel<DeepLinkProcessingViewModel>()
LaunchedEffect(Unit) {
    val deepLink = deepLinkProcessingViewModel.processDeepLinkIfAvailable(context) ?: return@LaunchedEffect
    if (deepLink == "example://playground") {
        navController.navigateBottomNavigationScreen(BottomNavigationScreen.ScreenB)
    }
}
NavHost(
    navController = navController,
    startDestination = BottomNavigationScreen.ScreenA.route
) {

findActivity

fun Context.findActivity(): Activity? = when (this) {
    is Activity -> this
    is ContextWrapper -> baseContext.findActivity()
    else -> null
}

1
投票

看起来它终于在 2.4.0-beta02 版本中修复了;所以这毕竟是一个错误。

我能够将 saveState 和 restoreState 命令添加回我的 BottomBar(根据文档),并且按照深层链接我现在仍然能够单击初始目的地。

© www.soinside.com 2019 - 2024. All rights reserved.