android - Fragments with setHasOptionsMenu in ViewPager get onCreateOptionsMenu called multiple times -


so have container fragments called "container" framelayout.

in container place fragment viewpager called collectionsfragment.

collectionsfragment has 3 fragments:

  • allcollectionsfragment - sethasoptionsmenu(true)
  • mycollectionsfragment - no menu
  • favouritecollectionsfragment - no menu

allcollectionsfragment has list of collections in , spinner menu in action bar , when user clicks on collection fragment called viewcollectionfragment placed in activity container so:

allcollectionsfragment -> onclick(position) -> viewcollectionfragment (placed in main activity container)

the problem viewcollectionfragment not have menu. has sethasoptionsmenu(false). android still calls allcollectionsfragment oncreateoptionsmenu when viewcollectionfragment displayed , menu of allcollectionsfragment still displaying when viewcollectionfragment visible.

i using fragmentstatepageradapter , passing getchildfragmentmanager() of collectionsfragment adapter.

i have onbackstackchangelistener activity container invalidates options menu whenever backstack changes so: collectionsfragment -> allcollectionsfragment (default) -> select collection -> add viewcollectionsfragment in backstack , in main container-> invalidateoptionsmenu -> allcollectionsfragment.oncreateoptionsmenu still called , menu displayed

any ideas why or should give more explanation?

i made workaround "problem". hiding menu in main activity (i check if fragment there tag) , check in fragment if backstack > 0 , hide menu...


Comments