[方案]Fragment引起的视图重叠问题

重现场景: 横竖屏切换

解决方式1:
暴力解决

@Override
 protected void onSaveInstanceState(Bundle outState) {
 }

解决方式2:
我的首界面上有2个Fragment,打电话和发短信的,通过底部BottomNavigationView切换

    private Fragment mContent;
    private Fragment mTabCallFragment;
    private Fragment mTabSMSFragment;

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        super.initView(savedInstanceState);

        initFragment(savedInstanceState);
        initBottomBar(savedInstanceState);
    }

    private void initFragment(@Nullable Bundle savedInstanceState) {
        if (savedInstanceState != null) {
            mTabCallFragment = getSupportFragmentManager()
                    .findFragmentByTag(CallFragment.class.getSimpleName());
            mTabSMSFragment = getSupportFragmentManager()
                    .findFragmentByTag(SMSFragment.class.getSimpleName());
            if (!mTabCallFragment.isHidden())
                mContent = mTabCallFragment;
            if (!mTabSMSFragment.isHidden())
                mContent = mTabSMSFragment;
            return;
        }
        mTabCallFragment = new CallFragment();
        mTabSMSFragment = new SMSFragment();
        FragmentTransaction addAction = getSupportFragmentManager().beginTransaction();
        addAction.add(R.id.container_content, mTabCallFragment, CallFragment.class.getSimpleName())
                .add(R.id.container_content, mTabSMSFragment, SMSFragment.class.getSimpleName())
                .commit();
        FragmentTransaction showAction = getSupportFragmentManager().beginTransaction();
        showAction.hide(mTabSMSFragment).show(mTabCallFragment).commit();
        mContent = mTabCallFragment;
    }

    private void initBottomBar(@Nullable Bundle savedInstanceState) {
        BottomNavigationView navigation = findViewById(R.id.navigation);
        navigation.setOnNavigationItemSelectedListener(mItemSelectedListener);
        if (savedInstanceState != null) {
            if (!mTabCallFragment.isHidden()) {
                navigation.setSelectedItemId(R.id.navigation_call);
            } else {
                navigation.setSelectedItemId(R.id.navigation_sms);
            }
        }
    }
    
    public void switchContent(Fragment from, Fragment to) {
        if (mContent != to) {
            mContent = to;
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            if (!to.isAdded()) {    // 先判断是否被add过
                transaction.hide(from).add(R.id.container_content,
                        to, to.getClass().getSimpleName()).commit(); // 隐藏当前的fragment,add下一个到Activity中
            } else {
                transaction.hide(from).show(to).commit(); // 隐藏当前的fragment,显示下一个
            }
        }
    }

你可能感兴趣的:([方案]Fragment引起的视图重叠问题)