0

我正在使用一个nav_graph,我有一个场景,比如我有fragment1,然后转到另一个fragment,它是一个子导航图,它有几个fragment(fragment3和fragment4),起始目的地是fragment3,从fragment3我要去fragment4 fragment4 有“完成”按钮。

这是场景:

  1. 我从fragment1和fragment4调用这个子导航图,我想重定向到它是父级,即fragment1
  2. 我从fragment2和fragment4调用这个子导航图,我想重定向到它是父级,即fragment2

这就是我在 fragment4 中处理完成按钮单击事件的方式:

findNavController().currentDestination?.parent?.startDestination?.let {
            findNavController().popBackStack(it, true)
        } 

但它没有重定向到父节点,而是重定向到子图统计目标,即片段3

这是我的 nav_graph 的样子:

这里有两个片段:

<fragment
        android:id="@+id/fragment1"
        android:name="fragment1"
        android:label="fragment1">

        <action
            android:id="@+id/action_fragment1_to_sub_graph"
            app:destination="@id/sub_graph_id" />
</fragment>

<fragment
        android:id="@+id/fragment2"
        android:name="fragment2"
        android:label="fragment2">

        <action
            android:id="@+id/action_fragment2_to_sub_graph"
            app:destination="@id/sub_graph_id" />
</fragment>

这是我的子导航图:

<navigation
        android:id="@+id/sub_graph_id"
        app:startDestination="@id/fragment3">

        <fragment
            android:id="@+id/fragment3"
            android:name="fragment3"
            android:label="fragment3">
            
            <action
                android:id="@+id/action_fragment3_to_fragment4"
                app:destination="@id/fragment4" />

        </fragment>

        <fragment
            android:id="@+id/fragment4"
            android:name="fragment4"
            android:label="fragment4"/>     
</navigation>

我错过了什么?我应该如何实现这一目标?

4

0 回答 0