Set animation listener to Activity animations

29,149

Solution 1

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 2

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 3

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 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.
    }
Share:
29,149
akc
Author by

akc

programmer

Updated on July 09, 2022

Comments

  • akc
    akc almost 2 years

    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 of achieving this, if not please suggest me some work around.