'How to change menu items on action bar when fragment changes

I developed a navigation drawer which has number of fragment,s and action bar. I want to know items on action bar change when i change the fragment .



Solution 1:[1]

you can access your menu items once you have set up "on create Options Menu as follows

 override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
        inflater.inflate(R.menu.menu_collections_new, menu)

        val menuItem2:MenuItem=menu.findItem(R.id.men_item2)
        menuItem2.title="new title2"
        

        super.onCreateOptionsMenu(menu, inflater)



    }/

Cheers...

Solution 2:[2]

Can you please try this

// declare a menu object at the top of the activity
Menu AppMenu;

// instantiate the AppMenu instance in the activity’s 
onCreateOptionsMenu(Menu menu)
// with code similar to:
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.myMenuLayout, menu); 
AppMenu = menu;
    return true;
}
// declare a menu object at the top of the activity
Menu AppMenu;

// instantiate the AppMenu instance in the activity’s 
onCreateOptionsMenu(Menu menu)
// with code similar to:
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.myMenuLayout, menu);
AppMenu = menu;
    return true;
}

// in the button click handler or menu item code, switch the title as 
needed
AppMenu.findItem(R.id.menuitem_expense_accept).setTitle("Expenses");// or 
“Accept”


// in the button click handler or menu item code, switch the title as 
needed
AppMenu.findItem(R.id.menuitem_expense_accept).setTitle("Expenses");// or 
“Accept”

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dharman
Solution 2 nassim