【问题标题】:Fragments getting dettached from Tabs when invalidateoptions menu is invoked调用 invalidateoptionsmenu 时,片段与选项卡分离
【发布时间】:2014-01-14 07:04:44
【问题描述】:

我的应用程序有三个选项卡,每个选项卡使用一个片段来显示其内容。我正在使用FragmentPageAdapterViewPager。我的第二个标签片段有两个子片段。在第二个子片段中,我显示了另外两个选项卡。

Tab1             Tab2         Tab3
Fragment1      Fragment2     Fragment3

       ChildFragment1 or ChildFragment2

                         ChildTab1          ChildTab2
                    ChildFragment1    ChildFragment3

再详细一点:

  • Tab2 包含 Fragment2。

  • 在 Fragment2 内部有一个条件。如果条件为真,则显示 childFragment1,否则显示 childframent2。

  • 在 childfragment2 中,我正在显示两个选项卡 childtab1 和 childtab2 childtab1 显示 childfragment1,childtab2 显示 childfragment3。

  • 在 Fragment2 中,我使用 getchildfragmentmanager()。

  • 在 childFragment2 中,我再次使用 getchildfragmentmanager 并使用 fragmentTabs 显示选项卡。

在我的主要活动中,我根据特定条件显示ActionItem。如果代码为真,则显示操作项,否则将其删除。要更新选项菜单,我正在使用 invalidateOptionsMenu() 和 actionitem 显示隐藏逻辑在 onPrepareOptionsMenu() 中实现:

MenuItem alertIcon = menu.findItem(R.id.alertIcon)
if(condtition)
   alertIcon.setVisibility(true);
else
   alertIcon.setVisibility(false);

执行此代码时,Fragment1 和 Fragment3 从选项卡中消失。但 Fragment2 的内容仍然可见。

我无法确定此问题的根本原因,请帮助。

【问题讨论】:

    标签: android android-fragments android-actionbar


    【解决方案1】:

    经过大量挖掘,我终于找到了解决上述问题的方法。我从 UI 线程外部调用 invalidateOptionsMenu()。所以我只是将调用修改为:

    this.runOnUIThread(new Runnable){
       @Override
       public void run(){
           invalidateOptionsMenu();
       }
    });
    

    我在我的活动中收到的回调正在运行到另一个线程中,从主线程以外的线程更新 UI 导致了问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-02
      • 1970-01-01
      • 1970-01-01
      • 2018-04-13
      • 1970-01-01
      • 1970-01-01
      • 2017-05-15
      • 1970-01-01
      相关资源
      最近更新 更多