Jetpack 导航 多个后退堆栈 底部菜单,一个图形,错误的选定项目行为

Jetpack Navigation Multiple backstack Bottom Menu, one graph, wrong selected item behavior

提问人:Joao 提问时间:11/15/2023 更新时间:11/15/2023 访问量:12

问:

我想为我的项目实现多个后退堆栈。 出于测试目的,我创建了一个默认的底部导航项目,并只添加了一个嵌套片段来测试它是如何工作的。我发现在某些情况下,底部菜单无法像我预期的那样工作。

示例:我的底部菜单中有 3 个菜单项:主页、仪表板和通知。 我创建了一个额外的 Deep Fragment,然后我启动了我的应用程序,打开了 Home Fragment,我转到了 Deep Fragment,然后转到了通知选项卡,还转到了 Deep Fragment,在这种情况下,多个后退堆栈按预期工作,它保存了 Fragment 的状态,但是当我切换回 Home Fragment 时, 底部菜单中的主页图标不会突出显示。 在这种情况下,我的导航.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 导航 喷气背包

评论


答:

0赞 Celil Uysal 12/5/2023 #1

选项 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>