Skip to content Skip to sidebar Skip to footer

Set Animation Listener To Activity Animations

I am using overridePendingTransition method to perform custom Activity animations. I would like to know when the animation ends ( a callback/listener ). Is there any direct way o

Solution 1:

I use this method to start any animation (resID of the animation XML). If nextPuzzleOnEnd is true, the method "nextPuzzle" is called when the animation has finished.

The method is part of my puzzle-apps and I use it to display any success animation and afterwards (after anim has finished) continue with the next puzzle.

 /*
 * start animation (any view)
 */
 private void startAnimation(View v, int resId, Boolean nextPuzzleOnEnd){
    Animation anim;

    if(v!=null){    // can be null, after change of orientation
            anim = AnimationUtils.loadAnimation(this.getContext(),resId);
            anim.setFillAfter(false);
            v.setAnimation(anim);
            if( nextPuzzleOnEnd ){
                anim.setAnimationListener(new AnimationListener() {
                    public void onAnimationStart(Animation anim)
                    {
                    };
                    public void onAnimationRepeat(Animation anim)
                    {
                    };
                    public void onAnimationEnd(Animation anim)
                    {
                        nextPuzzle();
                    };
                });                     
            }
            v.startAnimation(anim);                 
    }
  }

Solution 2:

After unsuccessfully browsing Google for this question, I've found a solution by going through all override-methods.

So what I did, was overriding this method in the activity that is entering the screen:

Requires API level 21

@Override
public void onEnterAnimationComplete() {
        super.onEnterAnimationComplete();
}

Solution 3:

overridePendingTransition does not have a listener. Like I wrote an earlier post you rather wanna use a normal animation instead for the overridePendingTransition (that is just for Android 2.0 and above).

You can get a similiar effect and you can also do more cool stuff with an ordinary animation. My earlier post here: Load XML slowly


Solution 4:

@Hambug solution is nice. But there is problem. onEnterAnimationComplete will work on Lollipop and above API (21).

@Override
public void onEnterAnimationComplete() {
    super.onEnterAnimationComplete();
    //write code here.
 }

whatever code you write in above method, won't execute on prelolipop devices. So you should put a version check and write it according to you need e.g. in onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if(Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP){
        //write code here.
    }

Post a Comment for "Set Animation Listener To Activity Animations"