我的应用程序架构有些独特,顺便说一下,我正在使用导航组件。
它有一个主活动,与之关联的是一个NavHostFragment。NavHostFragment带有与其相关联的导航图,其中包含两个片段,Main Fragment(作为起始目标)和Settings Fragment(可通过工具栏中的菜单项访问)。现在,Main Fragments充当另一个NavHostFragment的主机。因此,这就是它的样子:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.main.fragments.MainFragment">
<androidx.fragment.app.FragmentContainerView
android:id="@+id/child_nav_host"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@+id/bottom_navigation_bar"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/child_nav_graph" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_navigation_bar"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/bottom_nav_menu" />
</androidx.constraintlayout.widget.ConstraintLayout>
请注意,主片段底部有一个底部导航栏,但主活动中没有这样的底部导航栏。还要注意,主片段有另一个NavHostFragment,它指向不同的导航图。
以下是主活动的布局文件:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.main.MainActivity">
<com.google.android.material.appbar.MaterialToolbar
android:id="@+id/materialToolbar"
style="@style/Widget.MaterialComponents.Toolbar.Primary"
android:layout_width="0dp"
android:layout_height="?actionBarSize"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<androidx.fragment.app.FragmentContainerView
android:id="@+id/parent_nav_host"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/materialToolbar"
app:navGraph="@navigation/main_nav_graph" />
</androidx.constraintlayout.widget.ConstraintLayout>
这是主要的导航图:
<?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/main_nav_graph"
app:startDestination="@id/main_fragment">
<fragment
android:id="@+id/settings_fragment"
android:name="com.dbtechprojects.cloudstatustest.ui.main.fragments.SettingsFragment"
android:label="Settings"
tools:layout="@layout/fragment_settings" />
<fragment
android:id="@+id/main_fragment"
android:name="com.dbtechprojects.cloudstatustest.ui.main.fragments.MainFragment"
android:label="Welcome"
tools:layout="@layout/fragment_main" />
</navigation>
这里是子导航图(child_nav_graph)
<?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/main_nav_graph"
app:startDestination="@id/aws_fragment">
<fragment
android:id="@+id/aws_fragment"
android:name="com.dbtechprojects.cloudstatustest.ui.main.fragments.AwsFragment"
android:label="AWS"
tools:layout="@layout/fragment_aws" />
<fragment
android:id="@+id/azure_fragment"
android:name="com.dbtechprojects.cloudstatustest.ui.main.fragments.AzureFragment"
android:label="Azure"
tools:layout="@layout/fragment_azure" />
<fragment
android:id="@+id/gcp_fragment"
android:name="com.dbtechprojects.cloudstatustest.ui.main.fragments.GcpFragment"
android:label="GCP"
tools:layout="@layout/fragment_gcp" />
</navigation>
因此,正如您所看到的,主片段是托管映射到底部导航栏的其他三个片段的内容。我采用这种方法是因为我希望我的设置片段在导航到它时隐藏底部导航栏。现在,底部导航栏显示在我想要显示它们的所有三个片段中,但不显示在设置片段中,正如我所期望的那样。
现在问题来了,当我打开应用程序时,会显示主片段。主片段包含底部导航栏,当我选择底部导航栏中的项目之一时,它会正确地导航到正确的片段。但是当我进入设置片段然后返回时,底部导航栏停止工作。以下是展示行为的视频:https://imgur.com/RpQZmhF 这是MainActivity.kt:https://gist.github.com/ac65ae323ca771d064562d18d9fac880 这是MainFragment.kt:https://gist.github.com/c700e33b4f04e078abf8c94db9bb6faf
childNavController
吗?因为 bnv 不使用 mainNavController。 - undefined