I am using Navigation components to move between fragments. I created a Fragment superclass used by all fragments with the following:
var hideMenu = false
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
menu.clear()
if (hideMenu) return
inflater.inflate(R.menu.standard_menu, menu)
....
On the main/start fragment logic sets hideMenu to true true so no menu displays. Good so far.
The user can click a number of different buttons to display other fragments that do not override hideMenu. The problem is that right after the 'target' fragment onCreateOptionsMenu is called ( hideMenu = false so the menu is inflated), the main/start onCreateOptionsMenu method is called. And since it has hideMenu = true, the menu is cleared. Bad, bad, app.
I see from other StackOverflow questions the navigation component supposedly replaces not adds fragments to the backstack. Is this not the case for the main/startup fragment? If so, any words of wisdom to get around this problem?