Fragment show add replace 管理帮助方法

public Fragment showOrCreateFragment(FragmentManager fragmentManager,
      int containerLayoutId,
      Class<? extends Fragment> fragmentClass,
      String tag,
      int enter,
      int exit,
      int popEnter,
      int popExit,
      boolean bAddToBackStack,
      Bundle args) {
    List<Fragment> all = fragmentManager.getFragments();
    List<Fragment> sameContainer = new ArrayList<Fragment>();
    Fragment targetFragment = null;
    if (all == null) {
      all = new ArrayList<Fragment>();
    }
    for (Fragment frag : all) {
      //寻找同一容器中的fragment
      if (frag != null && frag.getView() != null && frag.getView().getParent() != null && ((ViewGroup) frag.getView().getParent()).getId() == containerLayoutId) {
        if (frag.getClass() == fragmentClass && tag.equals(frag.getTag())) {
          targetFragment = frag; //找到本尊
        } else {
          sameContainer.add(frag); //找到同窗
        }
      }
    }
    //如果指定的fragment不存在,创建并显示出来,隐藏同容器中的其他fragment
    boolean isNew = false;
    if (targetFragment == null) {
      if (args == null) {
        targetFragment = Fragment.instantiate(ContextUtil.getContext(), fragmentClass.getCanonicalName());
      } else {
        targetFragment = Fragment.instantiate(ContextUtil.getContext(), fragmentClass.getCanonicalName(), args);
      }
      isNew = true;
    }
    //
    FragmentTransaction transaction = fragmentManager.beginTransaction();
    //if (enter != -1 && exit != -1 && popEnter != -1 && popExit != -1) {
      transaction.setCustomAnimations(enter, exit, popEnter, popExit);
    //}
    for (Fragment frag : sameContainer) {
      transaction.hide(frag);
    }
    if (isNew) {
      transaction.replace(containerLayoutId, targetFragment, tag);
      mFragmentShow = false;
    } else {
      mFragmentShow = true;
      transaction.show(targetFragment);
    }
    if (bAddToBackStack) {
      transaction.addToBackStack(null);
      //fix bug: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
      //            transaction.commit();
    }
    transaction.commitAllowingStateLoss();
    return targetFragment;
  }

  /**
   *
   * @param fragmentManager
   * @param containerLayoutId
   * @return
   */
  public void removeAllFragment(FragmentManager fragmentManager,
      int containerLayoutId){
    FragmentTransaction transaction = fragmentManager.beginTransaction();

    List<Fragment> all = fragmentManager.getFragments();
    List<Fragment> sameContainer = new ArrayList<Fragment>();
    Fragment targetFragment = null;
    if (all == null) {
      all = new ArrayList<Fragment>();
    }
    for (Fragment frag : all) {
      //寻找同一容器中的fragment
      if (frag != null && frag.getView() != null && frag.getView().getParent() != null && ((ViewGroup) frag.getView().getParent()).getId() == containerLayoutId) {
        sameContainer.add(frag); //找到同窗
      }
    }
    for (Fragment frag : all) {
      transaction.remove(frag);
    }
    transaction.commitAllowingStateLoss();
  }
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容