Android fragment隐藏和显示重叠问题解决(叠加显示)

项目场景:

项目首页使用RadioGroup+RadioButton+Fragment实现页面切换,出现了一个问题fragment会出现重叠问题,就是一个fragment显示了两层,
并不是必现问题。

经过排查发现是项目主页面Activity被销毁重建了,但是之前的fragment对象没有被回收导致。

原因分析:

在旋转屏幕后或者页面调到后台一段时间在打开,就会出现Fragment重叠显示,显示了两个相同fragment的效果。


解决方案:

第一步,在添加fragment的时候添加key。
 第二步,再onCreate里判断savedInstanceState是否为空,若不为空说 明是重建的activity,用key将fragment对象取出来即可。
  下面我们来看一下具体代码

    private FragmentManager fManager;
    private FragmentTransaction transaction;
    private List<Fragment> fragmentList = new ArrayList<>();
    private FragmentOne fragmentOne;
    private FragmentTwo fragmentTwo;
    private FragmentFour fragmentFour;
    private FragmentFive fragmentFive;
    private static final String FRAGMENT_ONE_KEY = "FragmentOne";
    private static final String FRAGMENT_TWO_KEY = "FragmentTwo";
    private static final String FRAGMENT_FOUR_KEY = "FragmentFour";
    private static final String FRAGMENT_FIVE_KEY = "FragmentFive";
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        fManager = getSupportFragmentManager();
        //如果activity销毁后重建,在这里拿出之前的Fragment对象。
        if (savedInstanceState != null) {
            fragmentOne = (FragmentOne) fManager.findFragmentByTag(FRAGMENT_ONE_KEY);
            fragmentTwo = (FragmentTwo) fManager.findFragmentByTag(FRAGMENT_TWO_KEY);
            fragmentFour = (FragmentFour) fManager.findFragmentByTag(FRAGMENT_FOUR_KEY);
            fragmentFive = (FragmentFive) fManager.findFragmentByTag(FRAGMENT_FIVE_KEY);
            initFragment(fragmentOne);
            initFragment(fragmentTwo);
            initFragment(fragmentFour);
            initFragment(fragmentFive);
        }
        initView();
    }

    private void initView() {
        fragmentOne = (FragmentOne) fManager.findFragmentByTag(FRAGMENT_ONE_KEY);
        if (fragmentOne == null) {
            fragmentOne = new FragmentOne();
            fragmentList.add(fragmentOne);
            hideOthersFragment(fragmentOne, true, FRAGMENT_ONE_KEY);
        } else {
            hideOthersFragment(fragmentOne, false, FRAGMENT_ONE_KEY);
        }
        radioGroup.setOnCheckedChangeListener((group, checkedId) -> {
            switch (checkedId) {
                case R.id.radio1:
                    if (fragmentOne == null) {
                        fragmentOne = new FragmentOne();
                        fragmentList.add(fragmentOne);
                        hideOthersFragment(fragmentOne, true, FRAGMENT_ONE_KEY);
                    } else {
                        hideOthersFragment(fragmentOne, false, FRAGMENT_ONE_KEY);
                    }
                    break;
                case R.id.radio2:
                    if (fragmentTwo == null) {
                        fragmentTwo = new FragmentTwo();
                        fragmentList.add(fragmentTwo);
                        hideOthersFragment(fragmentTwo, true, FRAGMENT_TWO_KEY);
                    } else {
                        hideOthersFragment(fragmentTwo, false, FRAGMENT_TWO_KEY);
                    }
                    break;
                case R.id.radio4:
                    if (fragmentFour == null) {
                        fragmentFour = new FragmentFour();
                        fragmentList.add(fragmentFour);
                        hideOthersFragment(fragmentFour, true, FRAGMENT_FOUR_KEY);
                    } else {
                        hideOthersFragment(fragmentFour, false, FRAGMENT_FOUR_KEY);
                    }
                    break;
                case R.id.radio5:
                    if (fragmentFive == null) {
                        fragmentFive = new FragmentFive();
                        fragmentList.add(fragmentFive);
                        hideOthersFragment(fragmentFive, true, FRAGMENT_FIVE_KEY);
                    } else {
                        hideOthersFragment(fragmentFive, false, FRAGMENT_FIVE_KEY);
                    }
                    break;
            }
        });
    }

下面是两个方法

 //将fragment对象添加进fragmentList
    public void initFragment(Fragment fragment) {
        if (fragment != null) {
            fragmentList.add(fragment);
        }
    }
	}
	 //加载fragment
    public void hideOthersFragment(Fragment showFragment, boolean isAdd, String key) {
        transaction = fManager.beginTransaction();
        if (isAdd) {
            //这里别忘了添加key
            transaction.add(R.id.Frame_ID, showFragment, key);
        }
        if (ObjectUtils.isNotEmpty(fragmentList)) {
            for (Fragment fragment : fragmentList) {
                if (showFragment.equals(fragment)) {
                    transaction.show(fragment);
                } else {
                    transaction.hide(fragment);
                }
            }
            transaction.commit();
        }
    }

你可能感兴趣的:(Android开发问题记录,android)