ActionItem 出现在方向改变时

ActionItem appears on orientation change

我有 3 个片段:A, B, CActionItem,称为 Filters。只有当 Fragment A 分别可见时,这些 Filters 才应该可见。

所以当 BC 被替换为 A 时我隐藏 Filters 并在 A 被替换回来时恢复它们。

除非我改变屏幕的方向,否则效果很好。 Filters 在它之后变得可见。

我尝试了什么:

in Activity 代表 Fragments navigation override onConfigurationChanged() 我检查可见的 Fragment 如果它的 BChideActionFilters():

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
....//other logic

//check if backStack empty
if (getSupportFragmentManager().getBackStackEntryCount() != 0) {
        //check top fragment by position and name in backstack
        //LessonsFragment - B; SingleLessonFragment - C
        if (getTopFragment().getClass().getName().equals(LessonsFragment.class.getName()) ||
                getTopFragment().getClass().getName().equals(SingleLessonFragment.class.getName())) {
            hideActionFilterItem();
        }
    }

那是行不通的。 方法hideActionItem():

    private void hideActionFilterItem() {
    View menuItemView = findViewById(R.id.action_filters);
    if (menuItemView != null) {
        menuItemView.setVisibility(View.GONE);
    }
}

尝试调试,它运行 hideActionItem()ActionItem 仍然可见。

我也试过躲在onPause();在片段 BConConfogChanged() 中。

可能有人遇到过这样的问题。请帮忙。

P.S。请告诉我是否需要更多代码。谢谢

改变方向时,视图会完全重新创建,因此您需要在 onCreate() 中处理您的情况。

要显示的片段由 FragmentManager 自动处理,但我认为您必须在每次创建视图时 "respecify" 隐藏过滤器。我建议您查看 Fragment A 是否可见,如果不可见,请调用您的 hideActionItem() 方法:

public void onCreate (){
...
//Get the Fragment A using the fragmentManager
  FragmentA frag = (FragmentA)  getFragmentManager().findFragmentByTag(the_tag_you_used_to_add_the_fragment);
  if(frag!=null){
    if(!frag.isVisible())
       hideActionItem();
    }
   else hideActionItem();
}