Kitesurfer
Kitesurfer

Reputation: 3561

Animate removal of an Fragment

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 :)

Upvotes: 4

Views: 2048

Answers (2)

BadMouse
BadMouse

Reputation: 51

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();

Upvotes: 5

Iliiaz Akhmedov
Iliiaz Akhmedov

Reputation: 877

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();
    }

})

Upvotes: 0

Related Questions