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();
}


上面的方法不适合于支付宝的支付, 尤其是在获取了支付宝的响应以后:
针对支付宝的情况,建议使用一个新的Activity去处理,通过startActivityForResult,这样就不会有异常抛出

你可能感兴趣的:(android)