• 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();
        }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
        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;
                }
            });
        }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51

    下面是两个方法

     //将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();
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

  • 相关阅读:
    xilinx zynq7系列加载器无法连接的原因&测试xilinx Zynq7开发板的加载器和芯片是否正常的快速方法
    网络空间安全未来就业前景和就业方向,看着六点
    Win系统服务器管理器打开方式
    【JVM】垃圾回收算法
    laravel框架 - 消息队列如何使用
    [传智杯 #5 初赛] I-不散的宴会
    这些4K手机、电脑壁纸网站,你一定要知道
    计算机毕业设计php_thinkphp_vue的校园论坛网站
    JFX11+Maven+IDEA 发布跨平台应用的完美解决方案
    Linux学习(1) ——Linux基本介绍
  • 原文地址:https://blog.csdn.net/weixin_37587365/article/details/133788423