MenuProvider: What is the equivalent of invalidateOptionsMenu?

33 Views Asked by At

What is the equivalent of requireActivity().invalidateOptionsMenu() in a fragment when using the new MenuProvider API?

When I get some new information at runtime, I need to refresh the menu in my fragment.

class MyFragment : Fragment() {

        override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
            super.onViewCreated(view, savedInstanceState)
        
            //... Rest of code not shown

            requireActivity.addMenuProvider(myMenuProvider, viewLifecycleOwner, Lifecycle.State.RESUMED)
        }
    }
 
    // ... Rest of code not shown


    fun onDataChanged() {
        //.. How do I refresh my menu using the `myMenuProvider` or the `MenuHost`?
    } 
}
1

There are 1 best solutions below

0
ianhanniballake On

The MenuHost API that you are using to call addMenuProvider has another method: invalidateMenu():

Invalidates the Menu to ensure that what is displayed matches the current internal state of the menu. This should be called whenever the state of the menu is changed, such as items being removed or disabled based on some user event.

So you'd call requireActivity().invalidateMenu().