gpt4 book ai didi

android - 在 Compose 中使用 DeepLink 会导致无法向后导航

转载 作者:行者123 更新时间:2023-12-04 23:56:47 25 4
gpt4 key购买 nike

我有一个 FirebaseMessagingService 子类,它接收通知有效负载,然后使用以下 PendingIntent 创建推送通知:

        TaskStackBuilder.create(this).run {
addNextIntentWithParentStack(
Intent(
Intent.ACTION_VIEW,
"eway://traversals_monthly_bill_details".toUri()
)
)
getPendingIntent(2345, PendingIntent.FLAG_UPDATE_CURRENT)
}
以下是我们导航的各个嵌套图的可组合项:
    composable(Screens.INTERMEDIATE_MONTHLY_BILLINGS.navRoute,
deepLinks =
listOf(
navDeepLink {
uriPattern =
"eway://traversals_monthly_bill_details"
}
)) {
IntermediateMonthlyBillings({ statements ->
val lastIndex = statements.lastIndex
val gson = GsonBuilder().create()
val statementsByYearJson = gson.toJson(statements)
navController.navigate("${Screens.MONTHLY_BILLINGS}/$statementsByYearJson/$lastIndex")
})
}
composable("${Screens.MONTHLY_BILLINGS.navRoute}/{statementsByYear}/{initialIndex}",
arguments = listOf(
navArgument("statementsByYear") {},
navArgument("initialIndex") {
type = NavType.IntType
}
)
) {
val gson = GsonBuilder().create()
val statementsByYear = gson.fromJson(
it.arguments?.getString("statementsByYear"),
AllStatementsByYear::class.java
)
MonthlyBillings(statementsByYear, it.arguments?.getInt("initialIndex")!!)
updateCurrentScreen(Screens.MONTHLY_BILLINGS)
}
这是中间屏幕:
@Composable
fun IntermediateMonthlyBillings(
onDataAcquired: (AllStatementsByYear) -> Unit,
myEwayLoggedInViewModel: MyEwayLoggedInViewModel = get()
) {
val statementsByYear by myEwayLoggedInViewModel.statementsByYear.observeAsState(null)

if (statementsByYear == null) {
GenericLoader(type = MyLoaderType.LIGHT_BACKGROUND)
}
LaunchedEffect(statementsByYear == null) {
if (statementsByYear != null) {
onDataAcquired(statementsByYear!!)
}
}
}
问题是,虽然深层链接有效,但我被导航到中间屏幕并有回调 onDataAcquired在我收到数据后运行(然后将我导航到 MonthlyBillings 屏幕),看起来后台堆栈中充满了 MonthlyBillings 或中间屏幕的实例,我不知道为什么。
在我到达深度链接的目的地后,我需要向后退按钮发送垃圾邮件,大约 10 次尝试后,它会开始将我导航回原始堆栈。
我真正想要的是堆栈只有最终目标屏幕(MonthlyBillings)以及如果用户手动导航(例如 ScreenA、ScreenC、ScreenF、MonthlyBillings)将具有的一系列后台条目。

最佳答案

在 NavGraph 的中间体 composable 中记录 backQueue 目标路由后就好了像这样的目的地:

 composable(Screens.INTERMEDIATE_MONTHLY_BILLINGS.navRoute,
deepLinks =
listOf(
navDeepLink {
uriPattern =
"eway://traversals_monthly_bill_details"
}
)) {
Log.d("PAYMENTSGRAPH", "composable called with backstack ${navController.backQueue}")
navController.backQueue.forEach {
Log.d("PAYMENTSGRAPH", it.destination.route.toString())
}
IntermediateMonthlyBillings({ statements ->
Log.d("CALLBACK_PAYMENTSGRAPH", "callback called")
val lastIndex = statements.lastIndex
val gson = GsonBuilder().create()
val statementsByYearJson = gson.toJson(statements)navController.navigate("${Screens.MONTHLY_BILLINGS}/$statementsByYearJson/$lastIndex")
})
}
每当我打开深层链接时,我都会注意到以下输出:

2022-07-05 11:24:27.802 16304-16304/[REDACTED]D/PAYMENTSGRAPH: null16304-16304/[REDACTED] D/PAYMENTSGRAPH:splash_screen_graph16304-16304/[REDACTED] D/PAYMENTSGRAPH:splash_screen16304-16304/[REDACTED] D/PAYMENTSGRAPH:payments_graph16304-16304/[REDACTED] D/PAYMENTSGRAPH:payments_traversalsD/PAYMENTSGRAPH: intermediate_monthly_billings16304-16304/[REDACTED] D/PAYMENTSGRAPH:monthly_billings/{statementsByYear}/{initialIndex}


然后我意识到自从 D/PAYMENTSGRAPH: intermediate_monthly_billingsmonthly_billings/{statementsByYear}/{initialIndex} 导航返回后,目的地仍在堆栈中目的地,中间屏幕中的逻辑将再次运行,因此它正在为monthly_billings 屏幕重新创建NavBackStackEntry 并将我导航回那里。如果我足够快地向后退按钮发送垃圾邮件,我可以“超越”游戏,因此它有时可以“工作”。
修复很简单 navController.popBackStack()在从回调中导航到monthly_billings 屏幕之前。这是最终的代码:
    composable(Screens.INTERMEDIATE_MONTHLY_BILLINGS.navRoute,
deepLinks =
listOf(
navDeepLink {
uriPattern =
"eway://traversals_monthly_bill_details"
}
)) {
IntermediateMonthlyBillings({ statements ->
val lastIndex = statements.lastIndex
val gson = GsonBuilder().create()
val statementsByYearJson = gson.toJson(statements)
navController.popBackStack()
navController.navigate("${Screens.MONTHLY_BILLINGS}/$statementsByYearJson/$lastIndex")
})
}

关于android - 在 Compose 中使用 DeepLink 会导致无法向后导航,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72826135/

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