我有类别片段和子类片段用于输入要查看的类别子类。我在导航图xml中有操作来打开子类片段。如果我打开任何根类别并单击其任何子类别,那么如果单击的子曲库有子曲库,那么当用户单击它的子曲库时,我应该打开子类片段。有像树一样的方案:
根类别片段:
子类别片段:
下一个子类别片段:
当我单击最后一个子类别片段,其中与前一个父片段相同的片段(相同的操作)时,我得到以下错误:
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)
此处导航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中多次使用相同片段但新的多个实例吗?
发布导航图可能会有所帮助,但我认为您可以通过以下两种方式之一来解决此问题:
(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>