我的应用程序有三个选项卡,每个选项卡使用一个片段来显示其内容。我正在使用FragmentPageAdapter
和ViewPager
。我的第二个选项卡片段有两个子片段。在第二个子片段中,我又显示了两个选项卡。
Tab1 Tab2 Tab3
Fragment1 Fragment2 Fragment3
ChildFragment1 or ChildFragment2
ChildTab1 ChildTab2
ChildFragment1 ChildFragment3
更详细地说:
Tab2包含Fragment2。
碎片2内部存在一个条件。如果条件为true,则显示childFragment1,否则显示childframent2。
在childfragment2中,我显示了两个选项卡childtab1和childtab2childtab1显示childfragment1,childtab2显示childfraction3。
在Fragment2中,我使用getchildfragmentmanager()。
在childFragment2中,我再次使用getchildfragmentmanager,并使用fragmentTabs显示选项卡。
在我的主要活动中,我会根据特定条件显示ActionItem
。如果编码为true,则显示操作项,否则删除它。要更新选项菜单,我使用invalidateOptionsMenu()
,操作项显示隐藏逻辑在onPrepareOptionsMenu()
:中实现
MenuItem alertIcon = menu.findItem(R.id.alertIcon)
if(condtition)
alertIcon.setVisibility(true);
else
alertIcon.setVisibility(false);
当执行此代码时,Fragment1和Fragment3将从选项卡中消失。但碎片2的内容仍然可见。
我无法确定此问题的根本原因,请提供帮助。
经过大量挖掘,我终于找到了上述问题的解决方案。我从UI线程外部调用invalidateOptionsMenu()
。所以我简单地将调用修改为:
this.runOnUIThread(new Runnable){
@Override
public void run(){
invalidateOptionsMenu();
}
});
我在活动中收到的回调正在运行到另一个线程,从其他线程更新UI导致了问题。