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?
setCustomAnimations(enter, exit, popEnter, popExit) support enter and exit animation, So set four animations and must keep it before transaction.replace()
Replacing with an empty fragment, before the point of insertion of the next fragment and also delaying the insertion of the next fragment (by 200ms) so that the exit animation of the blank fragment can play, solved my problem.
This the code to insert an empty fragment with exit animation.
Exit.xml
pop_exit.xml
So the easy way:
When you open a fragment (called from parent Activity):
Specify enter and exit transactions
When closing a fragment (called from inside the fragment)
Specify enter and exit transactions
I saw this when I was having similar problems and just thought Id drop a quick note.
Rather than creating a dummy fragment in order to replace the existing one I think you should animate the current fragments view. When the animation finishes you can simply remove the fragment.
This is how i did it:
I got inspiration from Zoltish answer , this is my implementation:
1.add this method inside the fragment , it will animate the fragment out of the screen to the left:
The method that inside onAnimationEnd() removes the fragment like this:
2.call the animateOut of the fragment from onBack() of the activity.
Cheers
by the way my getDensity() is:
with it i calculate the DP value for the current running Device.