FragmentManager fragmentManager;
FragmentTransaction fragmentTransaction;
Fragment currentFragment;
для толкания фрагмента:
public void pushFragment(Fragment fragment, boolean addToBackStack, boolean shouldAnimate, Bundle bundle) {
currentFragment = fragment;
if (bundle != null) {
fragment.setArguments(bundle);
}
fragmentTransaction = fragmentManager.beginTransaction();
if (shouldAnimate) {
fragmentTransaction.setCustomAnimations(R.anim.enter_from_right, R.anim.exit_to_left, R.anim.enter_from_left, R.anim.exit_to_right);
} else {
fragmentTransaction.setCustomAnimations(R.anim.fade_in, R.anim.fade_out, R.anim.fade_in, R.anim.fade_out);
}
if (addToBackStack) {
fragmentTransaction.addToBackStack(fragment.getClass().getCanonicalName());
}
// Replace whatever is in the fragment_container dataView with this fragment,
// and add the transaction to the back stack so the user can navigate back
fragmentTransaction.replace(R.id.container, fragment, fragment.getClass().getCanonicalName());
// Commit the transaction
fragmentTransaction.commit();
}
для поп-фрагмента:
public void popBackFragment() {
try {
int backStackCount = fragmentManager.getBackStackEntryCount();
if (backStackCount > 1) {
FragmentManager.BackStackEntry backStackEntry = fragmentManager.getBackStackEntryAt(backStackCount - 2);
String className = backStackEntry.getName();
Fragment fragment = fragmentManager.findFragmentByTag(className);
currentFragment = fragment;
fragmentManager.popBackStack();
} else {
finish();
}
} catch (Exception e) {
e.printStackTrace();
}
}