0

我有 2 个导航主机活动 Activity A 和 Activity B,每个都有自己的导航图集。我想从 Activity A 托管的导航图中的 FragmentA 导航到 Activity B 托管的导航图中的 FragmentB,以完成我尝试显式深度链接的操作。但是,无论我如何无法检索 FragmentB 中的参数,它始终是默认值。我正在使用安全参数。我究竟做错了什么?

更新:似乎问题始于无法访问的导航图。仔细查看日志,我意识到 NavController 记录了以下消息

在导航图中找不到目的地 com.myapp.app:id/nav_graph_b,忽略来自 Intent 的深层链接......

导航图由在 setComponentName 中设置的导航主机活动 B 托管。为什么图表无法访问呢?

深层链接

val bundle = Bundle()
bundle.putInt("theIntKey", theInt)

val pendingIntent = NavDeepLinkBuilder(requireContext())
    .setComponentName(NavHostActivityB::class.java)
    .setGraph(R.navigation.nav_graph_b)
    .setDestination(R.id.fragmentB)
    .setArguments(bundle)
    .createPendingIntent()
    .send()

导航图

  <navigation xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/nav_graph_b"
    app:startDestination="@id/fragmentC"
    tools:ignore="UnusedNavigation">
    
        <fragment
            android:id="@+id/fragmentB"
            android:name="FragmentB">
            <argument
                android:name="theIntKey"
                app:argType="integer"
                android:defaultValue="0" />
        </fragment>
        
           <!--  other fragments-->
           
    </navigation>

内部片段B

//all of these three methods return always the default value
val theIntFromBundle = requireArguments().getInt("theIntKey")

private val args: FragmentBArgs by navArgs()

requireActivity().intent.extras?.let {
    val args = FragmentBArgs.fromBundle(it)
}
4

0 回答 0