Home > other >  Animating fragment transitions
Animating fragment transitions

Time:11-26

Im trying to make a fragment slide in/out on the x axis but for some reason its only animating when im entering the fragment. When going backwards it doesnt animate at all.

FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_right);
EditFragment edit = EditFragment.newInstance();
transaction.replace(android.R.id.content, edit, "editFragment");
transaction.addToBackStack("editFragment");
transaction.commit();

Is it maybe because im exiting the fragment this way?

getActivity().onBackPressed();

CodePudding user response:

You should use the setCustomAnimations method with four arguments:

public @NonNull FragmentTransaction setCustomAnimations(
    @AnimatorRes @AnimRes int enter,
    @AnimatorRes @AnimRes int exit,
    @AnimatorRes @AnimRes int popEnter,
    @AnimatorRes @AnimRes int popExit
)
  • Related