Prevent animation from repeating when changing the orientation of the device

Asked

Viewed 149 times

2

I’m working on a screen that contains an animation that is repeated every time the screen is rotated. I searched but found no precise information of what I need to do so the animation does not repeat when changing the orientation of the device.

This is the code of the Activity 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);
    }
}

Reading a little about the life cycle of an Activity, I saw that it is repeated every time the device is rotated. The question that remains is: which code need to be implemented so that the animation is not repeated?

1 answer

3


Where the guideline is amended Activity is recreated: the method onCreateis called.

A simple way for you to check your Activity was recreated is to check whether the Bundle passed to the method onCreate is void or not.

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

If you want the animation to only run at startup of the application put the instruction startAnimation within a if:

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

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

  • thank you very much!

  • I was testing here and realized that by checking if the Bundle savedInstanceState is null, the layout is reset to the state before the animation (it is as if the animation had not been executed). Could you tell me why this happens?

  • This is because the Activity is recreated or it is like it never existed. However Android uses the Bundle passed to the method onCreate to keep part of the state that the Activity had before being recreated. You can also use this Bundle to store whatever it deems necessary so that when the Activity is recreated, it is possible to put it in a state like the previous one, before the recreation. Hence the reason for the link I added to the answer.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.