I want to animate the removal of fragment.
I tried:
getSupportFragmentManager().beginTransaction()
.setCustomAnimations(R.anim.push_down_in, R.anim.push_up_out)
.remove(myFragment)
.commit();
but the fragment just disappears.
I noticed the out animation only plays with 'replace', so I tried to replace the fragment with an empty Fragment like this:
getSupportFragmentManager()
.beginTransaction()
.setCustomAnimations(R.anim.push_down_in, R.anim.push_up_out)
.replace(viewId, new Fragment())
.commit();
But it still just disappears disappears.
So, how can I animate the removal of fragment?
What enter:
it is new fragment which must be show
What exit:
it is current fragment which must be hide
What popEnter:
it is previous fragment which must be show
What popExit:
it is current fragment which must be hide
For use these animations, you should target them on show or hide transaction commands. Exit animations doesn't work on remove/replace procedures.
I agreed with hugoc and here some code to solve it
An easy fix below:
1-Call animate on fragment.getView().
2-Remove fragment inside onAnimationEnd().
I figured it out.
The exiting view is animated on the canvas of the entering view so if there is no entering canvas there is no canvas for the animation.
To show the animation I had to always use replace and use entering fragments of the same size to those exiting. After the animation is finished I set the view of the new fragments to gone.
reason as @hugoc said
below is actual code:
You could animate the removal by setting this custom animation to the fragmentTransaction
The third and fourth params are for removing the fragment