holder is your Activity that inherits 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(); }
When returning to the previous page:
public void backFragment (Fragment sourceFragment) { if (sourceFragment != null) { holder.fragmentManager.beginTransaction().hide(sourceFragment); } holder.fragmentManager.popBackStack(); }
When jumping to the next page and cannot go 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(); }
The above method is not suitable for Alipay payment, especially after obtaining the response of Alipay:
for the Alipay situation, it is recommended to use a new Activity to process, through startActivityForResult, so that no exception will be thrown