在进入不在底部导航视图中的片段并返回后,底部导航视图无法工作。

3

我的应用程序架构有些独特,顺便说一下,我正在使用导航组件。

它有一个主活动,与之关联的是一个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

你也可以对片段应用动画。或者你可以设置动画来隐藏底部视图。 - undefined
但是我的当前方法有什么问题呢?我不明白为什么它不起作用。 - undefined
看起来你的主导航控制器可能已被重置,或者我们需要调试它以找到实际原因。 - undefined
你是指 childNavController 吗?因为 bnv 不使用 mainNavController。 - undefined
让我们在聊天中继续这个讨论。 - undefined
显示剩余2条评论
2个回答

0

当开始为新片段进行事务时,请不要添加add

addtobackstack()

请使用这个代替

if (my_cart == null) {
                my_cart = new My_Cart();
                fragmentManager.beginTransaction()
                        .hide(currentFragment)
                        .add(R.id.main_fragment, my_cart)
                        .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE)
                        .commit();
            } else {
                fragmentManager.beginTransaction().hide(currentFragment).show(my_cart).setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN).commit();
            }
            currentFragment = my_cart;

0
问题出在我的MainFragment中。
    val bottomNavBar by lazy {
        binding.bottomNavigationBar
    }

我不应该再做其他的,而是应该直接使用binding.bottomNavigationBar。这样就解决了。


我正在尝试使用类似的方法,但在底部导航栏的片段列表中移动到活动片段时遇到了问题。希望你能帮忙解决。https://dev59.com/DsHqa4cB1Zd3GeqP14uB - user15587401

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接