Search code examples
android-studiokotlinandroid-actionbarbottomnavigationview

how to hide/remove icon menu items in actionbar when moving bottomnavigation kotlin?


in actionbar home fragment bottomnavigation

enter image description here

in action bar favorite fragment bottomnavigation

enter image description here

how to hide or remove actionbar icon when moving to favorite fragment?


Solution

  • You just need to make your FavoriteFragment options menu aware and then in onCreateOptionsMenu, simply clear the menu. this can be done by updating FavoriteFragment with following changes

    In onCreateView, call setHasOptionsMenu(true) to receive options menu related callbacks in Fragment

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                                  savedInstanceState: Bundle?): View? {
        // Allows your fragment to receive options menu related callbacks (onCreateOptionsMenu etc)
        setHasOptionsMenu(true)
        ...
    }
    

    now override onCreateOptionsMenu and clear the already populated menu as

    override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
        super.onCreateOptionsMenu(menu, inflater)
        menu.clear()
    }