目前我正在尝试实现启动时为 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
一切似乎都可以正常工作。
问题是,当用户按下后退按钮或在两个菜单项之间来回导航时,不会清除 backstack,这意味着在单击到 feature1 -> feature2 -> feature1 -> feature2 后,当用户按下后退按钮时导航如下: feature1 -> feature2 -> feature1 -> 在应用程序之外(原因popUpToInclusive
on SplashScreenFragment
),而所需的后退导航是: feature1 -> 在应用程序之外。
有人成功尝试使用BottomNavigationView
底部导航的第一项不是导航图的 startDestionation 吗?
已编辑,因为似乎问题是 startDestination 不包括图表