ViewPager 中带有 setHasOptionsMenu 的片段会多次调用 onCreateOptionsMenu

Fragments with setHasOptionsMenu in ViewPager get onCreateOptionsMenu called multiple times

所以我有一个名为 "container" 的片段容器,它是一个 FrameLayout。

在这个容器中,我放置了一个带有名为 CollectionsFragment 的 ViewPager 的片段。

CollectionsFragment 有 3 个片段:

AllCollectionsFragment 中有一个集合列表,操作栏中有一个微调菜单,当用户单击一个集合时,另一个名为 ViewCollectionFragment 的片段将放置在 activity 容器中,因此:

AllCollectionsFragment -> onClick(position) -> ViewCollectionFragment(放置在主 activity 容器中)

问题是 ViewCollectionFragment 没有菜单。它有 setHasOptionsMenu(false)。但是 Android 即使在显示 ViewCollectionFragment 时仍然调用 AllCollectionsFragment onCreateOptionsMenu 并且当 ViewCollectionFragment 可见时 AllCollectionsFragment 的菜单仍在显示。

我正在使用 FragmentStatePagerAdapter 并将 CollectionsFragment 的 getChildFragmentManager() 传递给适配器。

我还有一个用于 activity 容器的 onBackstackChangeListener,它会在 backstack 更改时使选项菜单无效,因此: CollectionsFragment -> AllCollectionsFragment(默认)-> Select collection -> 在后台堆栈和主容器中添加 ViewCollectionsFragment -> invalidateOptionsMenu -> AllCollectionsFragment.onCreateOptionsMenu 仍然被调用并显示菜单

有什么想法或者我应该给出更多解释吗?

我为此 "problem" 做了一个解决方法。我将菜单隐藏在我的主 activity 中(我通过它的标签检查片段是否存在)并且如果 backstack > 0 我检查片段并隐藏菜单...