0

Actually i am getting confused as the scope of the variables inside the method is limited so how come it can check for whether the animation is running or not wont it create a new instance every time a button is clicked as method is called when button is clicked. Can somebody explain it to me as this is working perfectly alright

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment);

         myView= (TextView) findViewById(R.id.label1);
       myButton= (Button) findViewById(R.id.myButton);
        newPlayer = MediaPlayer.create(this, R.raw.soundmusic);


        final EnlightenApp instance1 = new EnlightenApp();



        myButton.setOnClickListener(new View.OnClickListener(){

            public void onClick(View v){
                final String answer=instance1.EnlightenMe();
                myView.setText(answer);

                    DoAnimation();

            }

        });


    }

public void DoAnimation(){


        ImageView myImage= (ImageView) findViewById(R.id.imageView5);
        myImage.setImageResource(R.drawable.ball_animation);
        AnimationDrawable ballAnimation=(AnimationDrawable) myImage.getDrawable();

        if(ballAnimation.isRunning()){

            ballAnimation.stop();

        }

        ballAnimation.start();

    }
  • Are you talking about anonymous inner classes and closures? If so, an anonymous inner class has an implicit reference to the containing class instance so it can reference final instance variables and methods - more info https://stackoverflow.com/a/4732617/4252352 – Mark Aug 01 '17 at 19:11
  • Actually i want to ask that in the DoAnimation method i am checking whether the Animation is running or not if yes then stop it. so when this method is called wont the ballAnimation object will be assigned a new drawable object of particular resource id so ballAnimation.isRunning() should return false everytime ? – Huzaifa Ali Aug 01 '17 at 19:26
  • Right, yes it will be a new instance everytime, as it's a new Object, it will return false everytime in your `if` statement as it has not bee started yet - that is not to say that an Animation won't be running, just a different instance i.e when you click the button twice in quick succession. – Mark Aug 01 '17 at 19:42
  • Exactly So according to that ballAnimation.stop(); should never work but it is working this is why i am getting confused .The purpose of IF statement was that if i have clicked for the first time the animation starts running but after clicking it for the second time it checks if it is already running then stop it and start it again but the problem is if i remove the IF check ,the animation runs for the first time only .. – Huzaifa Ali Aug 01 '17 at 19:56
  • Not in a position to check the source, but docs say the instance, however if using `R.drawable.ball_animation` you're probably creating a reference to the same object, so that is why you are experiencing this issue - I'm guessing that you need to reset the animation when it completes, the XML needs one shot false? – Mark Aug 01 '17 at 20:07
  • yeah i got your point in short i need to reset the animation when it completes , was doing it in a bit of an odd manner so i will check the XML file for the changes .Thank you very much for your help much appreciated. – Huzaifa Ali Aug 01 '17 at 20:22

0 Answers0