Android NavController:如何使用相同的操作打开相同的片段

Android NavController: How to open same fragment with same action

我有Category片段和SubCategoryFragment,用于输入类别子项以供查看。我在导航图 xml 中执行操作以打开 SubCategoryFragment。如果我打开任何根类别并单击它的任何子目录,那么如果单击的子目录有子目录,那么我应该在用户单击子目录时打开 SubCategoryFragment。有树状方案:

根类别片段:

子类别片段:

下一个子类别片段:

当我单击最后一个次级子子类别片段时,我得到以下错误:

2019-10-23 16:48:03.472 24670-24670/com.example.store E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.store, PID: 24670
java.lang.IllegalArgumentException: navigation destination com.example.store:id/action_catalogPage_to_subCatsPage is unknown to this NavController
    at androidx.navigation.NavController.navigate(NavController.java:789)
    at com.example.store.helpers.NavigationExtensionsKt.navigateSafe(NavigationExtensions.kt:271)
    at com.example.store.helpers.NavigationExtensionsKt.navigateSafe$default(NavigationExtensions.kt:266)
    at com.example.store.fragments.catalog.SubCatsPage.onItemClick(SubCatsPage.kt:78)
    at com.example.store.helpers.adapters.catalog.SubCatsAdapter$SubCatsItemHolder$bindTo.onClick(SubCatsAdapter.kt:75)
    at android.view.View.performClick(View.java:7125)
    at android.view.View.performClickInternal(View.java:7102)
    at android.view.View.access00(View.java:801)
    at android.view.View$PerformClick.run(View.java:27336)
    at android.os.Handler.handleCallback(Handler.java:883)
    at android.os.Handler.dispatchMessage(Handler.java:100)
    at android.os.Looper.loop(Looper.java:214)
    at android.app.ActivityThread.main(ActivityThread.java:7356)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)

这里导航xml:

<?xml version="1.0" encoding="utf-8"?>
<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/catalog"
    app:startDestination="@id/catalogPage">

    <fragment
        android:id="@+id/catalogPage"
        android:name="com.example.store.fragments.catalog.CatalogPage"
        android:label="fragment_catalog_page"
        tools:layout="@layout/fragment_catalog_page" >
        <action
            android:id="@+id/action_catalogPage_to_subCatsPage"
            app:destination="@id/catalogSubCatsPage" />
        <action
            android:id="@+id/action_catalogPage_to_catalogShowCatProductsPage"
            app:destination="@id/catalogShowCatProductsPage" />
    </fragment>

    <dialog
        android:id="@+id/productDetailSheet"
        android:name="com.example.store.fragments.products.ShowProductDetailsBottomSheet"
        tools:layout="@layout/fragment_dialog_pr_detail_secondary">

        <argument
            android:name="productId"
            app:argType="string"
            android:defaultValue='"0"' />
        <deepLink
            android:id="@+id/deepLink4"
            app:uri="https://com.example/p/{productId}"
            android:autoVerify="true" />
    </dialog>

    <fragment
        android:id="@+id/catalogSubCatsPage"
        android:name="com.example.store.fragments.catalog.SubCatsPage"
        android:label="SubCatsPage" />

    <fragment
        android:id="@+id/catalogShowCatProductsPage"
        android:name="com.example.store.fragments.products.ShowCatProductsPage"
        android:label="fragment_show_cat_products_page"
        tools:layout="@layout/fragment_show_cat_products_page" />
</navigation>

有人知道如何在 NavController 中使用相同的片段但使用新的多个实例吗?

它可能对 post 您的导航图有帮助,但我认为您可以通过以下两种方式之一解决此问题:

(1) 类别片段可用的本地操作,或 (2) 对整个图可用的全局动作。

<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/nav_graph"
    app:startDestination="@id/category">

    <fragment
        android:id="@+id/category"
        android:name="com.example.app.CategoryFragment">

        <argument
            android:name="categoryId"
            app:argType="string"/>

        <!-- (1) Local action to self (category fragment) -->
        <action
            android:id="@+id/open_category"
            app:destination="@id/category"/>

    </fragment>

    <!-- (2) Global action to category fragment-->
    <action
        android:id="@+id/open_category_global"
        app:destination="@id/category"/>

</navigation>

在这两种情况下,都应创建片段的新实例。

您只需将目标 ID 设置为与片段 ID 相同即可。

喜欢下面

<fragment
        android:id="@+id/externalProfileFragment"
        android:name="com.social.footprint.ui.external_user_profile.ExternalProfileFragment"
        android:label="ExternalProfileFragment"
        tools:layout="@layout/external_profile_fragment">
        <argument
            android:name="user_id"
            app:argType="string" />
        <action
            android:id="@+id/action_externalProfileFragment_to_externalProfileFragment"
            app:destination="@id/externalProfileFragment" />


</fragment>