gpt4 book ai didi

deep-linking - Jetpack 撰写 : Bottom bar navigation not responding after deep-linking

转载 作者:行者123 更新时间:2023-12-04 17:17:04 29 4
gpt4 key购买 nike

我在我的新 Jetpack Compose 应用程序中设置了一个底栏,其中包含 2 个目的地。我尝试遵循 Google 的示例。

例如它看起来像这样:

@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)但是按下返回返回到ScreenA 的 startDestination。

我目前的解决方法是从示例代码中删除 restoreState = true 行。

我怀疑有关深层链接的某些内容一直存在,尽管它试图转到 ScreenA,但导航组件表示它有一个指向 ScreenB 的深层链接,所以它只是转到那里。我尝试过重置事件意图,使其在意图中没有标志和数据,我什至尝试过更改意图操作类型,但都无济于事。

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

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

最佳答案

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

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

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
}
}

并且使用此 View 模型,您可以像这样计算起始目的地:

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
}

关于deep-linking - Jetpack 撰写 : Bottom bar navigation not responding after deep-linking,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68456471/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com