Pop the fragment backstack without playing the Pop-Animation

pop fragment from backstack
setcustomanimations
android popbackstack to specific fragment
popbackstackimmediate
android replace fragment without backstack
android smooth fragment transition
pop all back stack android fragment
remove fragment from backstack

I push a fragment on the fragment stack using the following code:

FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_right,
     R.anim.slide_in_left, R.anim.slide_out_left);
fragmentTransaction.replace(getId(), newFragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();

This way, when the fragment stack is popped, e.g. by pressing the back button, a fragment pop animation is played. However, there are situations in which i would like to pop the fragment backstack without showing this animation, e.g. because I just returned from another activity and want to display the previous fragment at once, without animation.

An example navigation could look like this:

  • The user is on the start screen with the root fragment
  • He selects an item on the root fragment which then displays a new fragment to show details of that item. It does so using a fragment transaction that sets animations both for the push and the pop case (so when the user presses the back button, the transition is animated)
  • From this fragment he starts an activity which (for whatever reason) deletes the item that was just shown
  • When this activity finishes, I would like to return to the root fragment without showing the "pop animation" of the "detail fragment"

Is there a way to pop the fragment backstack without playing the specified 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.

Pop the fragment backstack without playing the Pop, This way, when the fragment stack is popped, e.g. by pressing the back button, a fragment pop animation is played. However, there are situations in which i� This way, when the fragment stack is popped, e.g. by pressing the back button, a fragment pop animation is played. However, there are situations in which i would like to pop the fragment backstack without showing this animation, e.g. because I just returned from another activity and want to display the previous fragment at once, without animation.

The user is on the start screen with the root fragment

Lets say the root fragment is contained in Activity A.

He selects an item on the root fragment which then displays a new fragment to show details of that item. It does so using a fragment transaction that sets animations both for the push and the pop case (so when the user presses the back button, the transition is animated)

The transaction is added to the back stack. Which means that when the back button is pressed from detail fragment, the popping process is animated.

From this fragment he starts an activity which (for whatever reason) deletes the item that was just shown.

Lets say it is Activity B

When this activity finishes, I would like to return to the root fragment without showing the "pop animation" of the "detail fragment"

One way of getting this behavior is by doing this in Activity B :

Intent intent = new Intent(this, A.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();

This will start the Activity A resetting it to its root state according to the documentation.(check the last paragraph in the section which says "This launch mode can also be used to good effect in conjunction with FLAG_ACTIVITY_NEW_TASK:......")

With this configuration, the animation will be present in the default case while in the special case you can control the animation using :

intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);

Which starts new activity without any animations. If you do want any animation, you can do it using the overridePendingTransition method.

How to pop fragment off backstack, @Akki if you are using add to backstack it will already pop the last fragment is popped, e.g. by pressing the back button, a fragment pop animation is played. i would like to pop the fragment backstack without showing this animation, e.g.� Cependant, il y a des situations dans lesquelles je voudrais poper le fragment backstack sans montrer cette animation, par exemple parce que je viens de revenir d'une autre activité et que je veux afficher le fragment précédent immédiatement, sans animation.

So for the support library following works:

In the fragment which should have a custom pop animation you override the onCreateAnimation with your own custom one. You could get it and set some kind of parameter depending on what you want. There might need to be done some extra work to make it work with regular fragments.

Here is the example where I'm overriding it and changing the set duration:

@Override
public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
    Animation anim = (Animation) super.onCreateAnimation(transit, enter, nextAnim);
    if(!enter) {
        if(anim != null) {
            anim.setDuration(0); // This doesn't seem to be called.
            return anim;
        } else {
            Animation test = new TestAnimation();
            test.setDuration(0);
            return test;
        }
    }
    return anim;
}

private class TestAnimation extends Animation {

}

Animate transitions between destinations, Shared element transitions to a fragment destination; Shared element transitions to an activity destination. Apply pop animations to activity transitions an action that pops additional destinations off of the back stack when navigating. Note: When using shared elements transitions, you should not use the� How to pop fragment off backstack (2) I have an activity A, which calls fragment Bf, which calls fragment Cf. I want Bf to be placed in the backstack when Cf is called so that users can navigate back to it.

So, I'd like to suggest a small change to @Geoff's answer.

Instead of having a global static boolean, I'd rather have a local non-static one. This is what I came up with.

Create an interface

public interface TransitionAnimator {
    void disableTransitionAnimation();
    void enableTransitionAnimation();
}

Make the fragment implement that interface.

public class MyFragment extends Fragment implements TransitionAnimator {

    private boolean mTransitionAnimation;

    @Override
    public void disableTransitionAnimation() {
        mTransitionAnimation = false;
    }

    @Override
    public void enableTransitionAnimation() {
        mTransitionAnimation = true;
    }

    @Override
    public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
        Animation result;
        if (!mTransitionAnimation) {
            Animation dummyAnimation = new Animation() {
            };
            dummyAnimation.setDuration(0);
            result = dummyAnimation;
        } else {
            result = super.onCreateAnimation(transit, enter, nextAnim);
        }
        return result;
    }

And then, when you want to disable the transition animations for a fragment, just do

if (fragment instanceof TransitionAnimator) {
    ((TransitionAnimator) fragment).disableTransitionAnimation();
}

to enable them, just do

if (fragment instanceof TransitionAnimator) {
    ((TransitionAnimator) fragment).enableTransitionAnimation();
}

If you want to do the same for all the fragments in the fragment manager, just do

List<Fragment> fragments = getSupportFragmentManager().getFragments();
for (Fragment fragment : fragments) {
    if (fragment instanceof TransitionAnimator) {
        // disable animations
        ((TransitionAnimator) fragment).disableTransitionAnimation();
    }
}

Very similar, but without static fields.

android.app.FragmentManager.getFragments java code examples , Pop the fragment backstack without playing the Pop-Animation. int count = fm. getFragments().size(); BaseFragment f = (BaseFragment)fm. android - with - remove fragment from backstack customAnimation when calling popBackStack on a FragmentManager (1) In my activity, with the touch of a button, I replace the current fragment with a new fragment using a custom animation, like in this example.

Android actually now has a way to do this without the work around @Geoff answered.

To avoid the animation to run on popBackStack(), when inflating your fragments add .setReorderingAllowed(true) to your fragmentTransaction.

So for example:

supportFragmentTransaction.beginTransaction()
    .setReorderingAllowed(true)
    .addToBackStack(null)
    .setCustomAnimations(
        android.R.anim.fade_in,
        android.R.anim.fade_out,
        android.R.anim.fade_in,
        android.R.anim.fade_out
    )
    .replace(yourContainer.id, yourFragment)
    .commit()

You'll notice that if you set setReorderingAllowed(true), the pop animation would no longer play. The results are actually similar to the result of @Geoff's answer.

Support Fragment pop animation is not working, android - Support Fragment pop animation is not working, Old question, but if is popped, e.g. by pressing the back button, a fragment pop animation is played. i would like to pop the fragment backstack without showing this animation, e.g.� Pop the fragment backstack without playing the Pop-Animation ; How to determine when Fragment becomes visible in ViewPager ; Replacing ViewPager with Fragment-Then Navigating Back ; Android 4.2: back stack behaviour with nested fragments

关于android:在不播放Pop-Animation的情况下弹出片段回栈| 码农家园, You can, however, only delete and replace fragments which you've added via the FragmentManager, not ones you've initiated using XML. Call this code to� Pop the fragment backstack without playing the Pop-Animation Best practice for instantiating a new Android Fragment Fragment onResume() & onPause() is not called on backstack

Android: Fragments: Replacing with back stack and animation, I push a fragment on the fragment stack using the following code:我使用以下代碼在片段堆棧上推送一個片段: FragmentMana. Pop the fragment backstack without playing the Pop-Animation ; How to determine when Fragment becomes visible in ViewPager ; Replacing ViewPager with Fragment-Then Navigating Back ; Android 4.2: back stack behaviour with nested fragments

Pop the fragment backstack without playing the Pop-Animation, I must confess: my true nemesis is not Fragments themselves, despite their quirky lifecycle. The real enemies are the Activity backstack and task navigation, and the animate the fragments according to state change directionfor all keys in the A button that says 'Get it on, Google Play', and if clicked. Pop the fragment backstack without playing the Pop-Animation ; Fragment onResume() & onPause() is not called on backstack ; How can I maintain fragment state when added to the back stack? Problems with Android Fragment back stack

Comments
  • What do you mean by I just returned from another activity? Can you tell the transition steps i.e., how you are trying to navigate.
  • Hi 500865, I added an example navigation to the question.
  • doesn't setting 0 as 3rd and 4th argument in setCustomAnimations do this?
  • this would disable animations in every case, however I usually want the animation, and setCustomAnimations has to be called when the fragment is pushed on the stack.
  • You can also try do it like [this][1] [1]: stackoverflow.com/a/18015081/855884
  • This works but the fragment will still show up for a brief frame. Is there a way to have it not show up at all?
  • +1 seems to be the only thing that works for me on 2.3 and 4.3 :)
  • +1 One must keep in mind this only works with popBackStackImmediate (it does not work with popBackStack).
  • For this to work using the regular version of popBackStack (instead of popBackStackImmediate), you'd have to avoid "unsetting" the FragmentUtils.sDisableFragmentAnimations flag until the fragments had actually been popped (i.e. you'd need some kind of callback to unset it).
  • @Geoff overriding onCreateAnimation throws .lang.IllegalStateException: super.onCreateAnimation(transit, enter, nextAnim) must not be null. Why?
  • Thanks for your answer! There is just one problem for my specific app: The Activity A is contained inside a TabHost which is created by my "main activity" (let's call it C). Is there a way to reset A without resetting C?
  • If you check the first paragraph in the link I posted in the answer, you will see "all of the other activities on top of it will be closed". Because C is below A in the stack(Tabhost Activity will be at the bottom of the stack) it will not reset C.