Fragment的压栈和弹出

  跳转至某一个fragment的时候:

  holder是你的Activity继承了FragmentActivity

 
public void goFragment (Fragment targetFragment, Bundle bundle) {
  FragmentTransaction transaction = holder.fragmentManager.beginTransaction();
  if (bundle != null) {
       targetFragment.setArguments(bundle);
     }
transaction.replace(R.id.re_content, targetFragment, targetFragment.getClass().getName());
		transaction.addToBackStack(targetFragment.getClass().getName());
		transaction.commit();
	}


  返回到上一页时:

 
public void backFragment (Fragment sourceFragment) {
    if (sourceFragment != null) {
			holder.fragmentManager.beginTransaction().hide(sourceFragment);
		}
		holder.fragmentManager.popBackStack();
	}


   跳转至下一页且不能back的时候
	public void goFragmentNoBackStack (Fragment targetFragment, Bundle bundle) {
		holder.fragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
		FragmentTransaction transaction = holder.fragmentManager.beginTransaction();
		if (bundle != null) {
			targetFragment.setArguments(bundle);
		}
		transaction.replace(R.id.re_content, targetFragment);
		transaction.commit();
	}

你可能感兴趣的:(Fragment的压栈和弹出)