onOptionsItemSelected not called

This problem can potentially also happen if using the menu attribute in XML:

        <com.google.android.material.appbar.MaterialToolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?android:attr/actionBarSize"
            app:menu="@menu/your_menu"
            app:navigationIcon="@drawable/..."
            app:title="@string/..." />

So app:menu="@menu/your_menu" doesn't work with the overridden fragment method onOptionsItemSelected. The menu item click listener has to be set to the Toolbar

private fun setMenuClickListener(toolbar: MaterialToolbar) = with(toolbar) {
        setOnMenuItemClickListener { menuItem ->
            if (menuItem.itemId == R.id.yourId) {
                //do something
                return@setOnMenuItemClickListener true
            }
             //this is a lambda so it can be just false,
             //added return to make it explicit
            return@setOnMenuItemClickListener false
        }
    }

So what we are looking for is the setOnMenuItemClickListener from MaterialToolbar


Just do the change as below :

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

In the onCreate(), call setSupportActionbar(), like so

toolbar = (Toolbar)findViewById(R.id.main_toolbar);
setSupportActionBar(toolbar);

Inside your onCreateOptionsMenu, return true instead of calling super. That should do it