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

mr.*_*fox 6 android android-fragments android-architecture-navigation

我有 Category fragment 和 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$1.onClick(SubCatsAdapter.kt:75)
    at android.view.View.performClick(View.java:7125)
    at android.view.View.performClickInternal(View.java:7102)
    at android.view.View.access$3500(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)
Run Code Online (Sandbox Code Playgroud)

这里导航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>
Run Code Online (Sandbox Code Playgroud)

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

小智 8

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

(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>
Run Code Online (Sandbox Code Playgroud)

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

  • 我使用这个解决方案,但对我不起作用!不要创建新片段! (4认同)