Android修改字体导致fragment事物hide,show

2018-06-25  本文已影响0人  Mr低丶调

1.现象
进入app,home键,然后修改手机字体大小,再进入app,点击切换tab,切换fragment出现各种重叠。
2.原因
查了点资料,以及自身掌握的知识,原来是应用启动了,修改字体,再回到activity,此时原activity会进入算是意外销毁,执行

   @Override
    protected void onSaveInstanceState(Bundle outState) {
        outState.putInt("position", position);
        super.onSaveInstanceState(outState);
    }

当再进入时,会在oncreate进行恢复,导致点击切换时,fragment对象是为空,但是界面是恢复的,所以提交事务的时候,全部hide()依然重叠
3.处理
(1)事务提交的时候,带上tag

  case BaseConstant.RB_ONE:
                if (homeFragment == null) {
                    homeFragment = new HomeFragment();
                    fragmentTransaction.add(R.id.fl_main_homepage, homeFragment, "one");
                } else {
                    fragmentTransaction.show(homeFragment);
                }
                break;
            case BaseConstant.RB_TWO:
                if (twoFragment == null) {
                    twoFragment = new TwoFragment();
                    fragmentTransaction.add(R.id.fl_main_homepage, twoFragment, "two");
                } else {
                    fragmentTransaction.show(twoFragment);
                }
                break;
            case BaseConstant.RB_THREE:
                if (threeFragment == null) {
                    threeFragment = new ThreeFragment();
                    fragmentTransaction.add(R.id.fl_main_homepage, threeFragment, "three");
                } else {
                    fragmentTransaction.show(threeFragment);
                }
                break;

(2)在oncreate处理

if (savedInstanceState != null) {
            FragmentManager manager = getSupportFragmentManager();
            homeFragment = (HomeFragment) manager.findFragmentByTag("one");
            twoFragment = (TwoFragment) manager.findFragmentByTag("two");
            threeFragment = (ThreeFragment) manager.findFragmentByTag("three");
            meFragment = (MeFragment) manager.findFragmentByTag("four");
            position = savedInstanceState.getInt("position");

        }

恢复的时候根据tag寻找fragment对象,这样当切换时,fragment对象不为空,提交前全部hide就生效不重叠
4.总结
学会处理activity的数据保存和恢复,希望对大家有帮助。

上一篇下一篇

猜你喜欢

热点阅读