正确切换Fragment

   private void switchFragment(Class clazz) {
        Fragment to = ViewUtils.createFragment(clazz);
        if (to.isAdded()) {
            Log.i(TAG, "Added");
            mFragmentManager.beginTransaction().hide(mCurrentFragment).show(to).commitAllowingStateLoss();
        } else {
            Log.i(TAG, "Not Added");
            try {
                mFragmentManager.beginTransaction().hide(mCurrentFragment).add(R.id.drawer_frame, to).commitAllowingStateLoss();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        mCurrentFragment = to;
    }

你可能感兴趣的:(正确切换Fragment)