Prevent repeat animation by changing device orientation

2

I'm working on a screen that contains an animation that is repeated every time the screen is rotated. I looked for but I did not find any precise information on what I need to do so that the animation does not repeat itself when changing the orientation of the device.

This is the Activity code in question:

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class Login extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        ImageView imgLogo = (ImageView) findViewById(R.id.imageView1);
        final LinearLayout LoginBox = (LinearLayout) findViewById(R.id.LoginBox);

        LoginBox.setVisibility(View.GONE);

        Animation animTranslate = AnimationUtils.loadAnimation(Login.this,
                R.anim.anim_logo);
        animTranslate.setFillAfter(true);
        animTranslate.setAnimationListener(new AnimationListener() {

            @Override
            public void onAnimationStart(Animation arg0) {
            }

            @Override
            public void onAnimationRepeat(Animation arg0) {
            }

            @Override
            public void onAnimationEnd(Animation arg0) {
                LoginBox.setVisibility(View.VISIBLE);
                Animation animFade = AnimationUtils.loadAnimation(Login.this,
                        R.anim.anim_login);
                LoginBox.startAnimation(animFade);

            }
        });
        imgLogo.startAnimation(animTranslate);
    }
}

By reading a little about the lifecycle of an Activity, I saw it re-enter every time the device is rotated. The question that remains is: which code do I need to implement so that the animation is not repeated?

    
asked by anonymous 16.06.2014 / 07:24

1 answer

3

Whenever orientation changes, your Activity is re-created: onCreate method is called.

A simple way to check if your Activity was recreated is to check whether the Bundle passed to the onCreate method is null or not.

When Activity is created for the first time savedInstanceState is null.

If you want the animation to be executed only at startup, put the startAnimation statement inside a if :

if(savedInstanceState == null){
    imgLogo.startAnimation(animTranslate);
}  

If you want to know more about using the Bundle passed to the onCreate method, follow this Link

    
16.06.2014 / 12:34