I need to add a menu depending on the fragment the user is looking at.
I tried to use the new MenuProvider
in the onViewCreated
-Method of my Fragment:
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val menuHost: MenuHost = requireActivity()
menuHost.addMenuProvider(object : MenuProvider {
override fun onCreateMenu(menu: Menu, menuInflater: MenuInflater) {
menuInflater.inflate(R.menu.menu_main, menu)
}
override fun onMenuItemSelected(menuItem: MenuItem): Boolean {
return true
}
}
}
This doesn't do anything with my Toolbar in the Main-activity. Do I have to enable menus somewhere?
Edit 1 As I saw in an answer to a similar problem, putting this into my MainActivity didn't do the trick:
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
return true
}
Edit 2 Moving my menu item into a group didn't fix it either.
Edit 3
I put a breakpoint at menuInflater.inflate()
but it wasn't activated, which tells me, the activity does not care about the menu provider.
I missed a setSupportActionBar(findViewById(R.id.toolbar))
in the onCreate
of my MainActivity