1. 程式人生 > >Fragment切換的兩種不同的寫法

Fragment切換的兩種不同的寫法

方式一:

 @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        Fragment currentFragment = fragmentManager.findFragmentByTag(fragmentTag);
        FragmentTransaction tr = fragmentManager.beginTransaction();
        tr.hide(currentFragment);
        switch (checkedId) {
            case R.id.rb_home:

                fragment = fragmentManager.findFragmentByTag(ChildHomeFragment.TAG);
                fragmentTag = ChildHomeFragment.TAG;
                if (fragment == null) {
                    fragment = new ChildHomeFragment();
                }

                break;
            case R.id.rb_msg:
                fragment = fragmentManager.findFragmentByTag(MessageFragment.TAG);
                fragmentTag = MessageFragment.TAG;
                if (fragment == null) {
                    fragment = new MessageFragment();
                }
                break;
            case R.id.rb_service:
                fragment = fragmentManager.findFragmentByTag(CustomerServiceFragment.TAG);
                fragmentTag = CustomerServiceFragment.TAG;
                if (fragment == null) {
                    fragment = new CustomerServiceFragment();
                }
                break;

            case R.id.rb_mine:
                fragment = fragmentManager.findFragmentByTag(MineFragment.TAG);
                fragmentTag = MineFragment.TAG;
                if (fragment == null) {
                    fragment = new MineFragment();
                }
                break;
        }
        if (fragment != null && fragment.isAdded()) {
            tr.show(fragment);
        } else {
            tr.add(R.id.content, fragment, fragmentTag);
        }
        Constant.SWITCH_FLAG = false;
        tr.addToBackStack(null);
        tr.commitAllowingStateLoss();
    }


    //初始化
    private void initFragmentManager() {
        fragmentTag = ChildHomeFragment.TAG;
        fragmentManager = getSupportFragmentManager();
        ft = fragmentManager.beginTransaction();
        fragment = fragmentManager.findFragmentByTag(ChildHomeFragment.TAG);
        fragment = new ChildHomeFragment();
        ft.add(R.id.content, fragment, fragmentTag);
        ft.addToBackStack(null);
        ft.commitAllowingStateLoss();
    }

    @Override
    public void onAttachFragment(Fragment fragment) {
        super.onAttachFragment(fragment);
        Constant.SWITCH_FLAG = false;
        if (homeFragment == null && fragment instanceof ChildHomeFragment) {
            homeFragment = (ChildHomeFragment) fragment;
        } else if (msgFragment == null && fragment instanceof MessageFragment) {
            msgFragment = (MessageFragment) fragment;
        } else if (csFragment == null && fragment instanceof CustomerServiceFragment) {
            csFragment = (CustomerServiceFragment) fragment;
        } else if (userFragment == null && fragment instanceof MineFragment) {
            userFragment = (MineFragment) fragment;
        }

    }

方式二:

 做個記錄