4

In an app, I try to animate the removal of a Fragment.

transaction.remove(fragmentVideo).remove(fragmentProgressBar).replace(R.id.content_pane_calling, endFragment);
transaction.setCustomAnimations(R.anim.slide_in, R.anim.slide_up);
transaction.commit();

The Framework ignores this completely. The Fragment itself gets removed, but the visual is not nice. Any FragmentTransaction#replace works well with those animations. I'm using the SupportLibrary v23.1.

Thanks for helping me out :)

user1506104
  • 6,554
  • 4
  • 71
  • 89
Kitesurfer
  • 3,438
  • 2
  • 29
  • 47

2 Answers2

5

The call to transaction.setCustomAnimation() has to be called BEFORE transaction.remove()/add()/replace() or the animations will never be run.

So your code will look like :

transaction.setCustomAnimations(R.anim.slide_in, R.anim.slide_up);
transaction.remove(fragmentVideo).remove(fragmentProgressBar).replace(R.id.content_pane_calling, endFragment);
transaction.commit();
BadMouse
  • 51
  • 2
  • Ah ok. It seems this question has been asked before http://stackoverflow.com/questions/13982240/how-to-animate-fragment-removal and you can't actually animate the removal of a fragment as the animation is run on the incoming view. The question I linked to has some ideas of how to get around this. – BadMouse Nov 06 '15 at 16:53
0

This is a pretty common issue I used to face. I am very careful when decide to use Fragments at all, since they are very unflexible as for UI animations. You might find an answer here:

http://daniel-codes.blogspot.com/2013/09/smoothing-performance-on-fragment.html

You could also animate your fragment and execute removal transaction after you are done with animation, doing both things separately in appropriate order.

//Pseudo Code

Animation anim = ... //this animates your fragment view
anim.setAnimationListener(new AnimationListener() {

    void onAnimFinish() {
        transaction.remove(fragmentVideo).remove(fragmentProgressBar).replace(R.id.content_pane_calling, endFragment);
        transaction.setCustomAnimations(R.anim.slide_in, R.anim.slide_up);
        transaction.commit()
        getFragmentManager().executePendingTransactions();
    }

})
Iliiaz Akhmedov
  • 867
  • 7
  • 17