目标片段中未收到 NavDeepLinkBuilder 参数包

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

我有 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)
}
android android-architecture-navigation android-navigation-graph android-deep-link
1个回答
0
投票

// 对于片段

如果您想在片段中获取参数(在您的情况下为 FragmentB),请确保您的 nav_graph 中有该 keyName (在您的情况下为 IntKey)。现在像下面这样

val argValue = argument?.getInt("theIntKey") // 对于 int,getString() 对于 string

// 您的 requireArguments().getInt("theIntKey") 也是正确的。获取默认值 0 可能表明“theInt”也是 0 确保它与默认值不同,否则代码对于片段来说是正确的。

// 活动

如果你想在活动中获得捆绑值,那么你可以通过一个小技巧在额外的中获取它,因为android封装参数只能直接访问片段,所以对于活动...

val EncapsulateddBundle = Intent.extras?.getBundle("android-support-nav:controller:deepLinkExtras")

val argValue = encapsulateddBundle.getInt("theIntKey")

您还可以通过以下方式获取片段内活动的意图:- requireActivity().intent.extras.......

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