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?
thank you very much!
– Renan Lazarotto
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?
– Renan Lazarotto
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.– ramaral