Can I add AnimationListener for Fragment translation

android, android-animation, android-fragments, animation

Solution

You can if you override `onCreateAnimation()` (or `onCreateAnimator()` if you are using 3.0+ fragments...both allow listeners) inside of your custom fragment to provide the animations rather than using the custom animation methods of `FragmentTransaction`:

@Override
public Animation onCreateAnimation (int transit, boolean enter, int nextAnim) {
    Animation anim;
    if (enter) {
        anim = AnimationUtils.loadAnimation(getActivity(), android.R.anim.fade_in);
    } else {
        anim = AnimationUtils.loadAnimation(getActivity(), android.R.anim.fade_out);
    }

    anim.setAnimationListener(new AnimationListener() {
        public void onAnimationEnd(Animation animation) { }

        public void onAnimationRepeat(Animation animation) { }

        public void onAnimationStart(Animation animation) { }
    });

    return anim;
}

Problem

I apply some animation for fragment translation. Can I add an animation listener to detect the animation start/end event? Thanks all.

Original source