android - 如何在 Android 导航组件中管理 BottomNavigationView 后台堆栈

标签 android android-fragments android-architecture-components bottomnavigationview android-jetpack

我正在使用 navigation componentBottomNavigationView ,我遇到了一个问题,那就是当我从 fragment 转到1>2>5>4>3,当我按下返回按钮时,我得到 fragment 1. 我知道这是默认行为,但我不想这样,我想将它们保存在 backstack所以当我按下返回按钮时,它应该转到 fragment 4 不是 1. 我一直在尝试和搜索,但找不到任何解决方案。我可以放fragment手动进入 backstackkotlin ?

我的代码:

activity_main.xml


<androidx.constraintlayout.widget.ConstraintLayout
           .......................
           .......................>

            <androidx.appcompat.widget.Toolbar
               ................
               ................/>

            <fragment
                android:id="@+id/app_nav_host_fragment"
                android:name="androidx.navigation.fragment.NavHostFragment"
                android:layout_width="0dp"
                android:layout_height="0dp"
                app:defaultNavHost="true"
                app:navGraph="@navigation/app_nav"
                ..............
                ............../>
            <com.google.android.material.bottomnavigation.BottomNavigationView
                android:id="@+id/app_bottom_nav_view"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:background="@drawable/white_grey_border_bottom"
                app:menu="@menu/bottom_nav_menu"
                app:labelVisibilityMode="unlabeled"
                ...........
                .........../>

</androidx.constraintlayout.widget.ConstraintLayout>

app_nav.xml


<navigation 
    ...........
    ...........
    android:id="@+id/app_nav"
    app:startDestination="@id/homeFragment">

    <fragment
        android:id="@+id/homeFragment"
        android:name="com.example.instagram_clone.ui.HomeFragment"
        android:label="HomeFragment"
        tools:layout="@layout/fragment_home"/>
    .............
    .............
    .............
    .............
</navigation>

MainActivity.kt


class MainActivity : AppCompatActivity()
{
    override fun onCreate(savedInstanceState: Bundle?)
    {
        super.onCreate(savedInstanceState)

        val binding  = DataBindingUtil.setContentView<ActivityMainBinding>(this, R.layout.activity_main)
        val bottomNavView = binding.appBottomNavView
        val navController = findNavController(R.id.app_nav_host_fragment)

        bottomNavView.setupWithNavController(navController)

    }
}

最佳答案

根据 onNavDestinationSelected() 上的文档(setupWithNavController() 在选择 MenuItem 时使用的方法):

By default, the back stack will be popped back to the navigation graph's start destination. Menu items that have android:menuCategory="secondary" will not pop the back stack.



所以只需添加 android:menuCategory="secondary"与您的 BottomNavigationView 一起使用的每个菜单项.

关于android - 如何在 Android 导航组件中管理 BottomNavigationView 后台堆栈,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58262507/

相关文章:

android - 随叫随到改造问题

android - 过滤由 View 模型的 LiveData 条目填充的回收器 View

android - PagedListAdapter 中的通用列表

android - 如果使用标准 Fragment,LiveData 不会调用 LifecycleRegistryOwner

java - 在退出时清理程序并处理 GC

android - 单击 Activity 上的后退按钮时,从未在数据库上显式调用 close()

java - fragment 中微调器的 setOnItemSelectedListener()

android - 仅显示一个 DialogFragment

java - 如何签署 update.zip

android - 使用 ContextMenu 从 listfragment 获取单个项目