Android 如何使用目标在导航图中多次出现的安全参数?

Android 如何使用目标在导航图中多次出现的安全参数?,android,android-architecture-navigation,android-navigation,android-navigation-graph,android-safe-args,Android,Android Architecture Navigation,Android Navigation,Android Navigation Graph,Android Safe Args,我使用SafeArgs很好地设置了应用程序的主导航。这是我的导航图.xml: <navigation android:id="@+id/nav_main" app:startDestination="@+id/fragment1"> <fragment android:id="@+id/fragment1" android:name="org.exam

我使用SafeArgs很好地设置了应用程序的主导航。这是我的导航图.xml

<navigation
    android:id="@+id/nav_main"
    app:startDestination="@+id/fragment1">

    <fragment
        android:id="@+id/fragment1"
        android:name="org.example.Fragment1"
        android:label="Fragment 1">

        <action
            android:id="@+id/fragment1_to_fragment2"
            app:destination="@id/fragment2" />

    </fragment>

    <fragment
        android:id="@+id/fragment2"
        android:name="org.example.Fragment2"
        android:label="Fragment 2">
        
        <argument
            android:name="ARG_CATEGORY_ID"
            app:argType="long"/>

        <action
            android:id="@+id/fragment2_to_fragment3"
            app:destination="fragment3" />
            
    </fragment>

    <fragment
        android:id="@+id/fragment3"
        android:name="org.example.Fragment3"
        android:label="Fragment 3">
        
        <argument
            android:name="ARG_ITEM_ID"
            app:argType="long"/>
        
   </fragment>

</navigation>
然而,我需要从通知中添加深度链接功能(具有带参数的backstack),因此我在nav_graph.xml的末尾添加了一个子
元素,它变成:

<navigation
    android:id="@+id/nav_main"
    app:startDestination="@+id/fragment1">

    <fra8gment
        android:id="@+id/fragment1"
        android:name="org.example.Fragment1"
        android:label="Fragment 1">

        <action
            android:id="@+id/fragment1_to_fragment2"
            app:destination="@id/fragment2" />

    </fragment>

    <fragment
        android:id="@+id/fragment2"
        android:name="org.example.Fragment2"
        android:label="Fragment 2">
        
        <argument
            android:name="ARG_CATEGORY_ID"
            app:argType="long"/>

        <action
            android:id="@+id/fragment2_to_fragment3"
            app:destination="fragment3" />
            
    </fragment>

    <fragment
        android:id="@+id/fragment3"
        android:name="org.example.Fragment3"
        android:label="Fragment 3">
        
        <argument
            android:name="ARG_ITEM_ID"
            app:argType="long"/>
        
    </fragment>
   
    <!-- This is the new sub navigation block that provides a backstack-with-arguments. -->
    <navigation
        android:id="@+id/nav_notification"
        app:startDestination="@id/fragment2Notif">

        <fragment
            android:id="@+id/fragment2Notif"
            android:name="org.example.Fragment2"
            android:label="Fragment 2">

            <action
                android:id="@+id/fragment2Notif_to_fragment3Notif"
                app:destination="@id/itemsGraph" />

        </fragment>

        <navigation
            android:id="@+id/itemsGraph"
            app:startDestination="@id/fragment3Notif">

            <argument
                android:name="ARG_ITEM_ID"
                app:argType="long"/>

            <fragment
                android:id="@+id/fragment3Notif"
                android:name="org.example.Fragment3"
                android:label="Fragment 3">

                <argument
                    android:name="ARG_ITEM_ID"
                    app:argType="long"/>

            </fragment>

        </navigation>
        
    </navigation>
   
</navigation>

请注意,该文件现在包含两个出现的
org.example.Fragment2

这给我带来了一个新问题,第二次出现的
org.example.Fragment2
(即fragment2Notif)导致原来的
Fragment2Directions.fragment2ToFragment3(…)
方法不再可用

它已被替换为
Fragment2Directions。Fragment2NotifyOfRagment3Notif(…)
在我的应用程序的通知部分工作正常,但在我的应用程序的主要部分调用时,正如预期的那样,会导致:

IllegalArgumentException:导航目的地 组织示例:id/fragment2Notif_to_fragment3Notif对此未知 导航控制器

似乎SafeArgs已经覆盖了第一个Fragment2Directions对象,这意味着它唯一的方法是
fragment2NotifToFragment3Notif(…)

那么,如何恢复旧的
fragment2ToFragment3(…)
方法呢


我希望我可以通过将Fragment2子类化来解决这个问题,以不同的名称有效地复制它,Fragment2Notif,然后在我的主导航中使用Fragment2,在子导航中使用Fragment2Notif,但是有更优雅/更喜欢的方法吗?

通过查看
nav_graph.xml
文件,我认为嵌套图(在父导航图中分组的一系列目的地,在本例中为id
nav_main
)的想法是添加来自通知的深度链接功能(具有带参数的backstack)

除此之外,假设SafeArgs插件将
fragment2ToFragment3(…)
覆盖为
fragment2NotifToFragment3Notif(…)
,没有重大变化,因为仍然可以使用
Fragment2NotifTofRagment3(…)导航到
Fragment3
因为两个嵌套的导航图组合在一起,除了
标记的id属性外,它们的结构内容与父导航图相同


因此,本质上,使用
fragment2NotifToFragment3Notif(…)
而不是
fragment2ToFragment3(…)
不会对这种情况造成伤害。

查看
nav_graph.xml
文件,我认为嵌套图的想法是正确的(在父导航图中分组的一系列目的地,在本例中为id
nav_main
)用于添加来自通知的深度链接功能(具有带参数的backbackback)

除此之外,假设SafeArgs插件将
fragment2ToFragment3(…)
覆盖为
fragment2NotifToFragment3Notif(…)
,没有重大变化,因为仍然可以使用
Fragment2NotifTofRagment3(…)导航到
Fragment3
因为两个嵌套的导航图组合在一起,除了
标记的id属性外,它们的结构内容与父导航图相同


因此,本质上,使用
fragment2NotifToFragment3Notif(…)
而不是
fragment2ToFragment3(…)
不会对这种情况造成伤害。

Hi!这是运行时异常还是编译异常?这是运行时异常,但完全可以预期/理解。主要问题是
fragment2ToFragment3()
方法丢失。您好!这是运行时异常还是编译异常?这是运行时异常,但完全可以理解。主要问题是
fragment2ToFragment3()
方法丢失。
<navigation
    android:id="@+id/nav_main"
    app:startDestination="@+id/fragment1">

    <fra8gment
        android:id="@+id/fragment1"
        android:name="org.example.Fragment1"
        android:label="Fragment 1">

        <action
            android:id="@+id/fragment1_to_fragment2"
            app:destination="@id/fragment2" />

    </fragment>

    <fragment
        android:id="@+id/fragment2"
        android:name="org.example.Fragment2"
        android:label="Fragment 2">
        
        <argument
            android:name="ARG_CATEGORY_ID"
            app:argType="long"/>

        <action
            android:id="@+id/fragment2_to_fragment3"
            app:destination="fragment3" />
            
    </fragment>

    <fragment
        android:id="@+id/fragment3"
        android:name="org.example.Fragment3"
        android:label="Fragment 3">
        
        <argument
            android:name="ARG_ITEM_ID"
            app:argType="long"/>
        
    </fragment>
   
    <!-- This is the new sub navigation block that provides a backstack-with-arguments. -->
    <navigation
        android:id="@+id/nav_notification"
        app:startDestination="@id/fragment2Notif">

        <fragment
            android:id="@+id/fragment2Notif"
            android:name="org.example.Fragment2"
            android:label="Fragment 2">

            <action
                android:id="@+id/fragment2Notif_to_fragment3Notif"
                app:destination="@id/itemsGraph" />

        </fragment>

        <navigation
            android:id="@+id/itemsGraph"
            app:startDestination="@id/fragment3Notif">

            <argument
                android:name="ARG_ITEM_ID"
                app:argType="long"/>

            <fragment
                android:id="@+id/fragment3Notif"
                android:name="org.example.Fragment3"
                android:label="Fragment 3">

                <argument
                    android:name="ARG_ITEM_ID"
                    app:argType="long"/>

            </fragment>

        </navigation>
        
    </navigation>
   
</navigation>