android - 单activity+多fragment场景,应用长时间在后台进入前台时,fragment全部是空白

 mobiledu2502875315 发布于 2022-10-28 14:05

1,问题:
应用长期在后台的场景下,进入前台时,fragment显示为空白
2,app框架大体实现:
1个activity+多个Fragment,使用的是add()方法以及 hide(),show()方法,显示fragment
3,相关代码:

onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // 判断 savedInstanceState是否为空:
    setCanResizeWindow(false);
    maskView = new View(getApplicationContext());
    maskView.setBackground(new ColorDrawable(Color.parseColor("#30000000")));
    maskView.setClickable(true);
    maskView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            removeMask();
        }
    });
    setContentView(R.layout.activity_home);

    EventBus.getDefault().register(this);

    //3,个推
    PushManager.getInstance().initialize(getApplicationContext());
    initView(savedInstanceState);
    getUserData();
    getDataFromMsgBrief();
}

    private void initView(Bundle savedInstanceState) {

        mBottomDial = (LinearLayout) findViewById(R.id.btn_Dial);
        mBottomComm = (LinearLayout) findViewById(R.id.btn_comm);
        mBottomContact = (LinearLayout) findViewById(R.id.btn_contact);
        mBottomLife = (LinearLayout) findViewById(R.id.btn_life);

        mBottomDialIv = (ImageView) findViewById(R.id.tab_dial_image);
        mBottomDiaTv = (TextView) findViewById(R.id.tab_dial_text);
        mSmsIV = (SmsImageView) findViewById(R.id.siv_sms_icon_homeact);
        //第一次登录系统时,修改数据库的Istravelrely字段
        if (!SpUtils.getSynDB(getApplicationContext())) {
            ThreadPoolUtil.getmExecutor().execute(new Runnable() {
                @Override
                public void run() {
                    CopyLocalContactsUtils.checkIsTravelRely(getApplication());
                    SpUtils.setSynDB(getApplicationContext());
                }
            });
        }

        //初始化mBottomDial为选中状态,fl_content填充DialFragment
        mBottomDial.setSelected(true);

//        if (savedInstanceState != null) {
//            LogUtils.d("HomeActivity savedInstanceState != null");
//
//            Fragment dialFragment = getSupportFragmentManager().findFragmentByTag(DIAL_TAG);
//            Fragment contactFragment = getSupportFragmentManager().findFragmentByTag(CONTACT_TAG);
//            Fragment commFragment = getSupportFragmentManager().findFragmentByTag(COMM_TAG);
//            Fragment lifeFragment = getSupportFragmentManager().findFragmentByTag(LIFE_TAG);
////            getSupportFragmentManager().beginTransaction()
//
//            getSupportFragmentManager().beginTransaction().hide(contactFragment)
//                    .hide(commFragment).hide(lifeFragment).show(dialFragment).commit();
//
//        } else {
            getSupportFragmentManager().beginTransaction().
                    add(R.id.fl_content, FragmentFactory.createFragment(0), "DialFragment").
                    add(R.id.fl_content, FragmentFactory.createFragment(1), "CommFragment").
                    add(R.id.fl_content, FragmentFactory.createFragment(2), "ContactFragment").
                    add(R.id.fl_content, FragmentFactory.createFragment(3), "LifeFragment").
                    hide(FragmentFactory.createFragment(1)).
                    hide(FragmentFactory.createFragment(2)).
                    hide(FragmentFactory.createFragment(3)).
                    show(FragmentFactory.createFragment(0)).commit();
//        }
        initClick();

    }

尝试过的解决办法:
1,为了解决多个fragment重叠显示,在activity的中执行了如下:

    @Override
    protected void onSaveInstanceState(Bundle outState) {
//        super.onSaveInstanceState(outState);
    }

问题现象:

3 个回答
  • 直接用ViewPager就行

    2022-10-29 16:41 回答
  • 内容有点乱,对于你这种需求,推荐使用FragmentTabHost,也就没有这种乱七八糟的问题了。

    2022-10-29 16:41 回答
  • @Override
        protected void onSaveInstanceState(Bundle outState) {
    //        super.onSaveInstanceState(outState);
        }

    这样的解决办法是不对的,这样会导致从activity被销毁是不能记住之前的显示内容,从而使界面空白、
    为了解决fragment重叠,你可以在注释掉super.onSaveInstanceState(outState)的同时,加上保存当前fragment的代码

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        outState.putString(SPConst.SHOW_TAG, showFragmentTag);
    }

    然后在回来的时候重新获取之前的fragment,然后再显示出来

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        if (savedInstanceState != null) {
            showFragmentTag = savedInstanceState.getString(SPConst.SHOW_TAG);
            if (showFragmentTag != null) {
                showFragment = fragmentManager.findFragmentByTag(showFragmentTag);              
                switch (showFragmentTag) {
                    case fragmentTag1:
                        workFragment = showFragment;
                        break;
                    case fragmentTag2:
                        patientFragment = showFragment;
                        break;
                    case fragmentTag3:
                        synergyFragment = showFragment;
                        break;
                    case fragmentTag4:
                        toolFragment = showFragment;
                        break;
                    case fragmentTag5:
                        mineFragment = showFragment;
                        break;
                }
            }
        }
    }
    2022-10-29 16:42 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有