hugoc hugoc - 10 months ago 58
Android Question

How to animate fragment removal

I want to animate the removal of fragment.

I tried:

.setCustomAnimations(R.anim.push_down_in, R.anim.push_up_out)

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:

.setCustomAnimations(R.anim.push_down_in, R.anim.push_up_out)
.replace(viewId, new Fragment())

But it still just disappears disappears.

So, how can I animate the removal of fragment?

Answer Source

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.