如何在具有导航组件的单个片段上正确添加选项菜单而不会破坏“向上行为" [英] How to properly add options menu on a single fragment with navigation component without broke "up behavior"

本文介绍了如何在具有导航组件的单个片段上正确添加选项菜单而不会破坏“向上行为"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在单个片段上添加选项菜单时遇到了一些麻烦,因为它破坏了导航.这是我的代码

I'm having some trouble to add options menu on a single fragment because it's breaking the navigation Up. Here my code

我有一个 NoActionBar 样式和这种布局的 Activity

I have an single Activity with NoActionBar style and with this layout

<layout 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:fitsSystemWindows="true"
    tools:context=".ui.MainActivity">

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/white">

        <fragment
            android:id="@+id/mainNavigationFragment"
            android:name="androidx.navigation.fragment.NavHostFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:defaultNavHost="true"
            app:navGraph="@navigation/main_graph" />

        <com.google.android.material.appbar.AppBarLayout
            android:id="@+id/appbarLayout"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:layout_gravity="top">

            <androidx.appcompat.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize" />

        </com.google.android.material.appbar.AppBarLayout>

        <com.google.android.material.bottomnavigation.BottomNavigationView
            android:id="@+id/bottomNavigationView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            app:labelVisibilityMode="labeled"
            app:menu="@menu/main_bottom_nav" />

    </androidx.coordinatorlayout.widget.CoordinatorLayout>

</layout>

在活动 onCreate 中,我为导航做了这个设置

in activity onCreate I make this setup for navigation

private fun setupNavigation() {
        val navController = findNavController(R.id.mainNavigationFragment)

        //each fragment of botton nav
        val appBarConfiguration = AppBarConfiguration(setOf(
                R.id.actionSchedule,
                R.id.actionPayment,
                R.id.actionNotification,
                R.id.actionAccount))

        toolbar.setupWithNavController(navController, appBarConfiguration)
        bottomNavigationView.setupWithNavController(navController)
    }

override fun onSupportNavigateUp() =
            findNavController(R.id.mainNavigationFragment).navigateUp()

在每个 botton 导航片段上,我都有一些目的地,一切都按预期工作.

on each botton nav fragment I have some destinations and everything work as expected.

现在我只需要在底部导航的最右边片段上添加一个菜单,然后在这个特定片段上我在 onCreate 中添加 setHasOptionsMenu(true) 并在 onCreateOptionsMenu,但没有出现菜单.

Now I need to add an menu only on right most fragment of botton nav, then on this specific fragment I add setHasOptionsMenu(true) in onCreate and inflate menu in onCreateOptionsMenu, but the menu does not appear.

然后我在活动 onCreate 上添加 setSupportActionBar(toolbar).

Then I add setSupportActionBar(toolbar) on activity onCreate.

现在菜单只出现在这个片段上,但它打破了任何目的地的所有向上"(工具栏上的后退箭头)(后退箭头出现,但当我按下时什么也没有发生).如果我删除 setSupportActionBar(toolbar) 活动,UP 会再次工作,但工具栏菜单不会.

Now the menu appear only on this fragment but it's broke all 'UP' (back arrow on toolbar) of any destination (the back arrow appears, but when i press nothing happens). If I remove setSupportActionBar(toolbar) of activity the UP work again but not the toolbar menu.

我需要做什么才能让菜单只在一个片段中工作而不破坏其他任何东西?谢谢

What I need to do to make the menu work only in one fragment without broke anything else? Thanks

推荐答案

如果你使用 setSupportActionBar,你必须使用 setupActionBarWithNavController(),而不是 toolbar.setupWithNavController 根据文档.

If you're using setSupportActionBar, you must use setupActionBarWithNavController(), not toolbar.setupWithNavController as per the documentation.

这篇关于如何在具有导航组件的单个片段上正确添加选项菜单而不会破坏“向上行为"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆