Pop the fragment backstack without playing the Pop-Animation

So Warpzit was on the right track, he just didn’t address your specific issue too well. I came across the exact same issue and here is how I solved it.

First I created a static boolean variable (for simplicity’s sake, lets put it in the FragmentUtils class)…

public class FragmentUtils {
    public static boolean sDisableFragmentAnimations = false;
}

Then, in EVERY fragment you have, you need to override the onCreateAnimation method…

@Override
public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
    if (FragmentUtils.sDisableFragmentAnimations) {
        Animation a = new Animation() {};
        a.setDuration(0);
        return a;
    }
    return super.onCreateAnimation(transit, enter, nextAnim);
}

Then, when you need to clear the backstack from your activity simply do the following…

public void clearBackStack() {
    FragmentUtils.sDisableFragmentAnimations = true;
    getSupportFragmentManager().popBackStackImmediate(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
    FragmentUtils.sDisableFragmentAnimations = false;
}

And voila, a call to clearBackStack() will drop you back into the root fragment without any transition animations.

Hopefully the big G will add a less stupid way of doing this in the future.

Leave a Comment