0

I have the following animation set in onClick. I am trying to start my activity after the animation is done, but it isn't working.

CODE

final ImageView iv1 = (ImageView) findViewById(R.id.imageView1);
iv1.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {

        Animation anim = AnimationUtils.loadAnimation(MainActivity.this, R.anim.animation);
        iv1.startAnimation(anim);

        Intent i = new Intent();
        i.setClass(MainActivity.this, P2.class);
        startActivity(i);
    }
});

QUESTION

How do I wait for the animation to complete before starting my activity?

zgc7009
  • 3,371
  • 5
  • 22
  • 34

2 Answers2

2

Use animation listener

Animation.AnimationListener listener = new Animation.AnimationListener() {
    @Override
    public void onAnimationStart(Animation animation) {

    }

    @Override
    public void onAnimationEnd(Animation animation) {
        Intent i = new Intent();
        i.setClass(MainActivity.this, P2.class);
        startActivity(i);
    }

    @Override
    public void onAnimationRepeat(Animation animation) {

    }
};
anim.setAnimationListener(listener);
iv1.startAnimation(anim);
  • while this should be accepted, an issue that i'm seeing is that the animation resets at the end of its animation. So if you fade something out, you'll get a few frames of it being visible before the activity launches. If you hackily set it to invisible in the `onAnimationEnd` it'll work, but it isn't a nice way to solve the issue. – DavidG Apr 09 '17 at 10:33
0

You can use AnimationListner for this. http://developer.android.com/reference/android/view/animation/Animation.AnimationListener.html

Here's an example: https://stackoverflow.com/a/8860057/3864698

Community
  • 1
  • 1
QArea
  • 4,955
  • 1
  • 12
  • 22