【发布时间】:2015-07-14 17:49:13
【问题描述】:
所以我有一个名为“container”的片段容器,它是一个 FrameLayout。
在这个容器中,我放置了一个名为 CollectionsFragment 的带有 ViewPager 的片段。
CollectionsFragment 有 3 个片段:
- AllCollectionsFragment - setHasOptionsMenu(true)
- MyCollectionsFragment - 无菜单
- FavoriteCollectionsFragment - 无菜单
AllCollectionFragment 中有一个集合列表,操作栏中有一个微调器菜单,当用户点击一个集合时,另一个名为 ViewCollectionFragment 的片段被放置在活动容器中,因此:
AllCollectionsFragment -> onClick(position) -> ViewCollectionFragment(放置在主活动容器中)
问题是 ViewCollectionFragment 没有菜单。它有 setHasOptionsMenu(false)。但是Android仍然会调用AllCollectionsFragment onCreateOptionsMenu,即使在显示ViewCollectionFragment并且ViewCollectionFragment可见时AllCollectionsFragment的菜单仍然显示。
我正在使用 FragmentStatePagerAdapter 并将 CollectionsFragment 的 getChildFragmentManager() 传递给适配器。
我还有一个用于活动容器的 onBackstackChangeListener,它使选项菜单在 backstack 发生变化时失效,因此: CollectionsFragment -> AllCollectionsFragment(默认) -> 选择集合 -> 在后台堆栈和主容器中添加 ViewCollectionsFragment -> invalidateOptionsMenu -> AllCollectionsFragment.onCreateOptionsMenu 仍然被调用并显示菜单
任何想法为什么或者我应该给出更多解释?
【问题讨论】:
-
活动/片段的孩子将始终将他们的选项菜单与他们父母的选项菜单结合起来。检查您的 MainActivity 是否有选项菜单。
-
是的,它有选项菜单。我还想提一下,我对主容器有 onBackStackChangeListener,每当更新 backstack 时,就会触发 supportInvalidateOptionsMenu()。这意味着当用户在 AllCollectionsFragment -> Press a collection -> Backstack change + invalidate -> ViewCollectionsFragment -> AllCollectionsFragment.onCreateOptionsMenu -> AllCollectionsFragment 菜单在 ViewCollectionsFragment 中仍然可见。所以你的意思是这很正常,因为孩子和父母之间的关系?
标签: android android-viewpager android-menu