以BottomNavigationView和SplashScreenFragment为startDestination的Android导航组件

时间:2020-02-11 15:22:43

标签: android bottomnavigationview android-architecture-navigation android-navigation-graph

当前,我正在尝试实施在启动时为SplashScreenFragment的应用程序,几秒钟后,我使用BottomNavigationView切换到屏幕

<?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/app_navigation"
    app:startDestination="@id/navigation_splash_screen">

    <fragment
        android:id="@+id/navigation_splash_screen"
        android:name="com.example.test.SplashScreenFragment"
        tools:layout="@layout/fragment_splash_screen">

        <action
            android:id="@+id/action_navigation_splash_to_feature1"
            app:destination="@id/feature1_navigation"
            app:popUpTo="@+id/navigation_splash_screen"
            app:popUpToInclusive="true" />
    </fragment>

    <fragment
        android:id="@+id/navigation_test_1"
        android:name="com.example.test.Fragment1" />

    <fragment
        android:id="@+id/navigation_test_2"
        android:name="com.example.test.Fragment2" />

<fragment
        android:id="@+id/navigation_test_3"
        android:name="com.example.test.Fragment3" />
</navigation>

然后我将bottom_nav_menu.xml定义如下:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@id/navigation_test_1"
        android:icon="@drawable/ic_dashboard_black_24dp"
        android:title="@string/bottom_nav_title_feature1" />

    <item
        android:id="@id/navigation_test_2"
        android:icon="@drawable/ic_notifications_black_24dp"
        android:title="@string/bottom_nav_title_feature2" />

<item
        android:id="@id/navigation_test_3"
        android:icon="@drawable/ic_notifications_black_24dp"
        android:title="@string/bottom_nav_title_feature3" />
</menu>

导航组件的版本:

androidx.navigation:navigation-fragment-ktx:2.2.0
androidx.navigation:navigation-ui-ktx:2.2.0

现在,在bottomNavigationView.setupWithNavController(findNavController(R.id.nav_host_fragment))内调用Activity且其中nav_host_fragment在活动布局内定义后,一切似乎都可以正常工作。

问题是,当用户按下向后按钮或在菜单的两个项目之间来回导航时,不清除后退堆栈,这意味着在单击功能1->功能2->功能1->功能2后,当用户按向后按钮导航时如下所示:功能1->功能2->功能1->在应用程序外部(由于popUpToInclusive上的SplashScreenFragment,而所需的向后导航是:功能1->应用程序外部。

任何人都成功尝试使用BottomNavigationView,其中底部导航的第一项不是开始导航图的说明?

编辑后,似乎是问题startDestination不包含在图中

0 个答案:

没有答案