Fragmentation icon indicating copy to clipboard operation
Fragmentation copied to clipboard

isSupportVisible()的问题

Open Anayuta opened this issue 7 years ago • 6 comments

private void dispatchSupportVisible(boolean visible) { if (visible && isParentInvisible()) { return; } if (mIsSupportVisible == visible) { mNeedDispatch = true; return; }

    mIsSupportVisible = visible;

    //
    if (visible) {
        if (checkAddState()) return;
        mSupportF.onSupportVisible();

        if (mIsFirstVisible) {
            mIsFirstVisible = false;
            mSupportF.onLazyInitView(mSaveInstanceState);
        }
        dispatchChild(true);
    } else {
        dispatchChild(false);
        mSupportF.onSupportInvisible();
    }
}

页面嵌套的fragment数量层级多了的时候,当前fragment的mIsSupportVisible 为true,但遍历到子的mFragment的时候得到父的isSupportVisible为false了。 private boolean isParentInvisible() { ISupportFragment fragment = (ISupportFragment) mFragment.getParentFragment(); return fragment != null && !fragment.isSupportVisible(); }

Anayuta avatar Dec 27 '18 11:12 Anayuta

然后导致子fragment的onSupportVisible()不回调了

Anayuta avatar Dec 27 '18 12:12 Anayuta

作者你好 VisibleDelegate里的方法 isParentInvisible(),其中 !fragment.isSupportVisible()改为!fragment.getSupportDelegate().isSupportVisible()就可以执行到。。

private boolean isParentInvisible() {
    ISupportFragment fragment = (ISupportFragment) mFragment.getParentFragment();
    return fragment != null && !/*fragment.isSupportVisible()*/fragment.getSupportDelegate().isSupportVisible();
}

Anayuta avatar Dec 28 '18 01:12 Anayuta

作者你好 VisibleDelegate里的方法 isParentInvisible(),其中 !fragment.isSupportVisible()改为!fragment.getSupportDelegate().isSupportVisible()就可以执行到。。

private boolean isParentInvisible() {
    ISupportFragment fragment = (ISupportFragment) mFragment.getParentFragment();
    return fragment != null && !/*fragment.isSupportVisible()*/fragment.getSupportDelegate().isSupportVisible();
}
 @Override
    final public boolean isSupportVisible() {
        return mDelegate.isSupportVisible();
    }

   @Override
    public SupportFragmentDelegate getSupportDelegate() {
        return mDelegate;
    }

难道不是同一个?

liaolintao avatar Mar 13 '19 10:03 liaolintao

@liaolintao 你页面多了的时候打断点跟一下就会发现,原本是true的fragment.isSupportVisible()得到的是false,fragment.getSupportDelegate().isSupportVisible()这个值里面得到的就是你想要的结果。开始我也一直以为是同一个,但是赋值也就那么几个地方,却出现了这问题。

Anayuta avatar Mar 13 '19 10:03 Anayuta

@Anayuta 源码写着的是同一个啊,你确定这样改了之后效果不一样?请参考liaolintao贴出来的源码。 我也碰到不回调的问题,但是情景和你们不一样

Simon1121 avatar Aug 15 '19 09:08 Simon1121

@Simon1121 我一开始也认同是同一个,根据源码看怎么都是同一个,断点跟也是同一个,但是Fragment嵌套过多的时候就会出现这情况,但是你会发现原本那个值自己知道是true的,fragment.isSupportVisible()这个得到是false,fragment.getSupportDelegate().isSupportVisible()这个得到是true,很是奇怪。

Anayuta avatar Aug 15 '19 10:08 Anayuta