Jetpack 导航多个后退底部菜单,一张图表,错误的所选项目行为

问题描述 投票:0回答:1

我想为我的项目实现多个backstacks。 出于测试目的,我创建了一个默认的底部导航项目,并仅添加了一个嵌套片段来测试它的工作原理。我发现在某些情况下底部菜单无法按我的预期工作。

示例:我在底部菜单中有 3 个菜单项:主页、仪表板和通知。 我创建了一个额外的 Deep Fragment,然后我启动了我的应用程序,打开了 home Fragment,我转到 Deep Fragment,然后转到通知选项卡,还转到了 Deep Fragment,在这种情况下,多个后堆栈按预期工作保存片段的状态,但是当我切换回主页片段时,底部菜单中的主页图标不会突出显示。 在本例中是我的 navigation.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/mobile_navigation"
    app:startDestination="@+id/navigation_home">

    <fragment
        android:id="@+id/navigation_home"
        android:label="@string/title_home"
        android:name="com.test.myapplication.ui.home.HomeFragment"
        tools:layout="@layout/fragment_home" />

    <fragment
        android:id="@+id/navigation_dashboard"
        android:label="@string/title_dashboard"
        android:name="com.test.myapplication.ui.dashboard.DashboardFragment"
        tools:layout="@layout/fragment_dashboard" />

    <fragment
        android:id="@+id/navigation_notifications"
        android:label="@string/title_notifications"
        android:name="com.test.myapplication.ui.notifications.NotificationsFragment"
        tools:layout="@layout/fragment_notifications" />


    <fragment
        android:id="@+id/DeepFragment"
        android:label="@string/title_notifications"
        android:name="com.test.myapplication.ui.notifications.NotificationsFragment2"
        tools:layout="@layout/fragment_notifications" />


</navigation>

我做了一些研究,也许我弄错了,但我是否需要为每个选项卡和每个片段包含一个图表。这样,底部菜单就可以正常工作了。但我不会在所有导航文件中重复片段,并且无法包含一些通用图表。 我无法从包含的一个图表导航到另一个图表,在我的应用程序中我有很多共享屏幕。 我无法包含一些带有共享片段的通用图表。 你们如何处理这一切?

android navigation jetpack
1个回答
0
投票

选项1:
您可以在

android:menuCategory="secondary"
中使用
bottom_nav_menu
。在这种情况下,不支持返回堆栈,碎片返回堆栈将丢失。

选项2:
使用嵌套图。该解决方案还有助于管理复杂的导航图。在这种情况下,你可以这样使用:

mobile_navigation.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"
        android:id="@+id/mobile_navigation"
        app:startDestination="@+id/navigation_home">
    
        <include app:graph="@navigation/navigation_dashboard" />
        <include app:graph="@navigation/navigation_notifications" />
        <include app:graph="@navigation/navigation_home" />
    </navigation>

navigation_home.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/navigation_home"
        app:startDestination="@id/homeFragment">
    
        <fragment
            android:id="@+id/homeFragment"
            android:name="com.example.myapplication.ui.home.HomeFragment"
            android:label="fragment_home"
            tools:layout="@layout/fragment_home" />
    </navigation>

navigation_dashboard.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/navigation_dashboard"
        app:startDestination="@id/dashboardFragment">
    
        <fragment
            android:id="@+id/dashboardFragment"
            android:name="com.example.myapplication.ui.dashboard.DashboardFragment"
            android:label="fragment_dashboard"
            tools:layout="@layout/fragment_dashboard" >
            <action
                android:id="@+id/action_dashboardFragment_to_navigation_detail"
                app:destination="@id/navigation_detail" />
        </fragment>
        <include app:graph="@navigation/navigation_detail" />
    </navigation>

navigation_notifications.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/navigation_notifications"
        app:startDestination="@id/notificationsFragment">
    
        <fragment
            android:id="@+id/notificationsFragment"
            android:name="com.example.myapplication.ui.notifications.NotificationsFragment"
            android:label="fragment_notifications"
            tools:layout="@layout/fragment_notifications" >
            <action
                android:id="@+id/action_notificationsFragment_to_navigation_detail"
                app:destination="@id/navigation_detail" />
        </fragment>
        <include app:graph="@navigation/navigation_detail" />
    </navigation>

navigation_detail.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"
        android:id="@+id/navigation_detail"
        app:startDestination="@id/notificationsFragment2">
    
        <fragment
            android:id="@+id/notificationsFragment2"
            android:name="com.example.myapplication.ui.deep.NotificationsFragment2"
            android:label="NotificationsFragment2" />
    </navigation>
© www.soinside.com 2019 - 2024. All rights reserved.