-1

I have added some animations on a view.
After launching an activity, all the animations are starting to animate. After stopping the animation effect I go to another activity that is in the same app. When I come back again to the activity that animation exists in, all animations start to animate again but it is not required.

I need to stop that animation effect after resume is called in an activity. I couldn't find any solutions for that. Any suggestions?

UPDATED: I adding all the animation init and starting in the onCreate() method.

UPDATED:

Animations are initiating in the onCreate() method

sparkButton.setVisibility(View.GONE);
        welcomeLayout.setVisibility(View.VISIBLE);
        AnimationSet animationSet = new AnimationSet(true);
        animationSet.setFillEnabled(true);
        animationSet.setInterpolator(new BounceInterpolator());
        Animation animation1 = AnimationUtils.loadAnimation(this, R.anim.welcome_slide_right_left);

        animation1.setDuration(700);
        animationSet.addAnimation(animation1);

        final AnimationSet animationSet2 = new AnimationSet(true);
        ScaleAnimation animation2 = new ScaleAnimation(1.0f, 0.14f, 1.0f, 1.0f, Animation.RELATIVE_TO_SELF, 1.0f, Animation.RELATIVE_TO_SELF, 0.0f);

        animation2.setDuration(400);
        animation2.setStartOffset(400);
        animationSet2.addAnimation(animation2);

        animationSet.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                welcomeLayout.setAnimation(animationSet2);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        animationSet2.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                welcomeLayout.setVisibility(View.GONE);
                sparkButton.setVisibility(View.VISIBLE);
                sparkButton.playAnimation();
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });

        welcomeLayout.setAnimation(animationSet);

I put the source as shown above. This issue needs to clarify and I will mark the answer as a hack solution. If whether this is an issue of Animation object that we didn't handle correctly, That's what I want to resolve

UPDATED: The button activity to go to another activity

        case R.id.spark_button: {
            // network checking code will append here
            // after that calls the activity
            startActivity(new Intent(SettingsActivity.this, HomeActivity.class));
            break;
        }
E_net4
  • 27,810
  • 13
  • 101
  • 139
E J Chathuranga
  • 927
  • 12
  • 27

4 Answers4

1

call clearAnimation() method for the View which is being animate in onStop() method of your activity not in onResume since it also trigger when activity launches for the first time

1

You could try as WorkAround to put your start Animation code inside a condition like this in onCreate

if(null == savedBundleState) {
 ////Play your Animation
 }
User One
  • 455
  • 3
  • 11
1

You can create Boolean and check it always when your activity starts;

boolean shouldShowAnimation = true;   

@Override
public void onResume(){       
    super.onResume();
    if(shouldShowAnimation) {
       shouldShowAnimation = false;
       // Your animation
    }
}
Rasul
  • 727
  • 7
  • 20
0

Call clearAnimation() on whichever View you called startAnimation().

Hope this helps!