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?
This is fairly easy to achieve through
overridePendingTransition(int enterAnim, int exitAnim)
with both0
for no animation.Override this in the fragment that you want to pop without animation and still keep the animation when you enter
Reply to Geoff and plackemacher comment.
You can try to remove all views from this Fragment. Then fragment will show but it should be transparent.
Remove all-1 (I use navigate drawer so drawer fragment should stay) fragment:
Sorry for my English
This is a follow-up to @Geoff's excellent answer, but fitted for a more dynamic and real-live scenario.
I imagined this being a nice little post, but I realize now that it got a little out of hand. However, the code is all there and I find it really useful, though it covers a lot more than just how to disable transition animations.
Usually, when I work with Fragments I like to have a BaseFragment that attaches to a BaseActivityCallback. This BaseActivityCallback can be used by the my Fragments to add a new Fragment on top of itself, or even to pop Fragments beneath it, hence the desire to disable pop animations -- or pop silently:
Now you can create your
MainActivity
and have that show aFragment1
which can add anotherFragment2
which may in turn popFragment1
silently: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)...
Then, in EVERY fragment you have, you need to override the onCreateAnimation method...
Then, when you need to clear the backstack from your activity simply do the following...
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.
Just use another overloaded method of
setCustomAnimation()
and in which do not set the R.anim.slide_out and that will solve your problemCheers :)