How to navigate from Service to a Navigation Fragment using probably Pending Intent?
我正在尝试使用通知中的 args 打开一个片段。
实际上,就我而言,我的音频播放器运行带有通知的前台服务,现在我想通过在用户单击通知时传递音频 ID 来导航到该特定音频的片段。
您可以使用 PendingIntent 打开您的活动,然后通过处理活动中的 Intent 打开您的片段。
或类似主题的这个答案
NavDeepLinkBuilder:
1
2 3 4 5 6 7 8 9 10 |
val pendingIntent = NavDeepLinkBuilder(context)
.setComponentName(YourActivity::class.java) .setGraph(R.navigation.your_nav_graph) .setDestination(R.id.your_destination) .setArguments(bundle) .createPendingIntent() //then notificationBuilder.setContentIntent(pendingIntent) |
- 当有很多导航时,这很笨拙。导航架构的人没有为此制作任何实用程序吗?我的意思是更容易处理带有 Intent 的导航。
- 你可以看看这个帖子。 stackoverflow.com/a/55245317/11181035
- 哦,做到了!谢谢,这应该是我问题的答案。
- 这真的是非常快速的答案,你们太棒了……再次感谢。
- 这重新创建了我的片段。有没有办法让我保留的片段生成?
- 我无法用这个灵魂重新创建后台。当我使用这个解决方案时,我有 Fragment A -> B -> C -> D -> E ,图表只有 A -> E 。任何的想法?
- 无法启动活动,在我的片段内显示空指针异常
- 我在我的服务中使用了 NavDeepLinkBuilder 并传递了服务上下文,但它不起作用你能告诉你如何在前台服务中使用 NavDeepLinkBuilder。
- 当面向新的 Android S 时,您必须使用 .createTaskStackBuilder() .getPendingIntent(0, FLAG_IMMUTABLE) 而不是 .createPendingIntent() 否则您的应用将由于新的 Pending Intent 限制而崩溃。
来源:https://www.codenong.com/55908724/