美文网首页
2020-03-12

2020-03-12

作者: 辣公公 | 来源:发表于2020-03-12 20:11 被阅读0次
    private synchronized void showFragment(Stack<Fragment> fragmentStack, Class cls, Bundle mBundle, String uuid) {
            if (fragmentStack != fragmentMap.get(mViewDataBinding.rgRadioGroup.getCheckedRadioButtonId()))
                return;
            Fragment fragmentTarget = getSupportFragmentManager().findFragmentByTag(uuid);
            FragmentTransaction tf = getSupportFragmentManager().beginTransaction();
            if (fragmentTarget == null) {
                fragmentTarget = fragmentNewInstance(cls, mBundle);
            }
            for (Fragment tmp : getSupportFragmentManager().getFragments()) {
                if (tmp instanceof SupportRequestManagerFragment) {
                    continue;
                }
                String tag = tmp.getTag();
                if (TextUtils.isEmpty(tag)) {
                    tag = UUID.randomUUID().toString();
                    try {
                        FieldUtils.writeField(tmp, "mTag", tag);
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    }
                }
                if (tmp.getTag().equals(uuid)) {
                    continue;
                }
                tf.hide(tmp);
            }
            //不包含
            if (fragmentStack.search(fragmentTarget) == -1) {
                fragmentStack.push(fragmentTarget);
                tf.add(R.id.fg_container, fragmentTarget, uuid).show(fragmentTarget);
            } else {
                //调整堆栈位置 栈顶元素问显示的fragment
                fragmentStack.remove(fragmentTarget);
                fragmentStack.push(fragmentTarget);
                tf.show(fragmentTarget);
            }
            removeOtherStack(fragmentStack, tf);
            tf.commitNowAllowingStateLoss();
        }
    

    相关文章

      网友评论

          本文标题:2020-03-12

          本文链接:https://www.haomeiwen.com/subject/diuwjhtx.html