Fragment popbackstack animation not working

Viewed 4955

I want to animate a fragment on opening and closing. I have a fade in and a fade out custom animation XML files.

I'm using setCustomAnimations on my support FragmentTransaction, but all it does is animating when I do the addToBackStack, when I do a popBackStack it just disappears without animation.

Here's a snippet of my code:

private void fragmentAppear(){
    fragment = new LoginFragment();
    fragmentManager = LoginActivity.this.getSupportFragmentManager();
    fragmentTransaction = fragmentManager.beginTransaction();
    //my XML anim files
    fragmentTransaction.setCustomAnimations(R.anim.slide_in_bottom,0,0,R.anim.slide_out_bottom);
    fragmentTransaction.replace(R.id.login_fragment, fragment);
    fragmentTransaction.addToBackStack(null);
    fragmentTransaction.commit();
}

private void fragmentDisappear(){
    getSupportFragmentManager().popBackStack();
}

On the setCustomAnimations part I'm using the 4 params and so far it only shows the fade out animation right before the slide in when I call fragmentAppear, but never when calling fragmentDisappear. I have already tried ordering the parameters in many, many different ways, also I have tried with the two param version of setCustomAnimations and all it does is animating when the fragment appears.

I'm using android.support.v4.app library for my fragments.

EDIT: Also, the animation won't show when pressing the back button without calling fragmentDisappear either.

The past code is in the activity, I tried to do a popBackStack from the fragment and it isn't working either. Is it the right way to close my fragment?

EDIT: I will include the XML animations:

slide_in_bottom.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:fromYDelta="75%p"
        android:toYDelta="0%p"
        android:fillAfter="true"
        android:duration="400" />
</set>

slide_out_bottom.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:fromYDelta="0%p"
        android:toYDelta="75%p"
        android:fillAfter="true"
        android:duration="400" />
</set>
3 Answers

If you look at the code you are replacing the fragment with a new fragment but you're actually setting add to back stack null. It's good practice to provide a tag for each fragment, and it's even going to be easy to find that fragment by tag as well. Add tags to your fragment like below. And if it still doesn't work then the problem will be inside your animation xml files.

private void fragmentAppear(){
   fragment = new LoginFragment();
   fragmentManager = LoginActivity.this.getSupportFragmentManager();
   fragmentTransaction = fragmentManager.beginTransaction();
   //my XML anim files
   fragmentTransaction.setCustomAnimations(
        R.anim.slide_in_bottom,0,0,R.anim.slide_out_bottom);
   fragmentTransaction.replace(
        R.id.login_fragment, fragment, "loginFragment");
   fragmentTransaction.addToBackStack("loginFragment");
   fragmentTransaction.commit();
}

From the Fragment Transaction documentation, I see this function, and there you have to specify appropriate animations.

/**
 * Set specific animation resources to run for the fragments that are
 * entering and exiting in this transaction. The
 * <code>popEnter</code>
 * and <code>popExit</code> animations will be played for enter/exit
 * operations specifically when popping the back stack.
 */
 public abstract FragmentTransaction setCustomAnimations(@AnimRes int enter,
        @AnimRes int exit, @AnimRes int popEnter, @AnimRes int popExit);
  1. enter => Animation when fragment enter
  2. exit => Animation when fragment exits.
  3. popEnter => Animation when fragment enters from back stack.
  4. popExit => Animation when fragment exit when popping from back stack.

Play with these until you get your desired behavior.

In my case, the fragment container height is wrap_content so the pop animation not work (while enter animation work well).
Set the fragment container height by a specific height or match_parent will make the pop animation work well

This code works for me. If you want to use this code in activity, delete at the beginning getActivity():

getActivity().getSupportFragmentManager()
            .beginTransaction()
            .setCustomAnimations(android.R.anim.slide_in_left, android.R.anim.fade_out,android.R.anim.slide_in_left, android.R.anim.fade_out)
            .replace(R.id.fragment_container, new YourFragment)
            .addToBackStack(null)
            .commit();
Related