Vlad Alexeev
Vlad Alexeev

Reputation: 2214

postDelayed Handler doesn't stop

what I need is when competition.isTimerStarted turns false this runnable should start buttonsProcedure() once and stop itself But this doesn't happen - buttonsProcedure() is being called forever and runnable doesn't stop.

 timerRun=new Runnable()
            {
                 @Override
                 public void run() {

                     timerTVmainmenue.setText(competition.getTimerTime());
                     if (!competition.isTimerStarted) {thandler.removeCallbacks(timerRun);Log.d("MyLog","timerrun is blame for infinite butproc");buttonsProcedure();}
                thandler.postDelayed(timerRun, 243); //looping this for every 243 ms
                 }
            };

how can I stop it properly?

Upvotes: 2

Views: 680

Answers (2)

Yordan Lyubenov
Yordan Lyubenov

Reputation: 191

Get it out of the runnable. What you are doing is removing callbacks and then (next line) -- posting it again. If you change the places you will reach your desired effect, but at the cost of always setting it up and removing callbacks. So just remove callbacks from somewhere else in your code.

Upvotes: 0

Blackbelt
Blackbelt

Reputation: 157447

        timerRun=new Runnable()
        {
             @Override
             public void run() {

                 timerTVmainmenue.setText(competition.getTimerTime());
                 if (!competition.isTimerStarted){
                thandler.removeCallbacks(this);
                Log.d("MyLog","timerrun is blame for infinite butproc");
                 buttonsProcedure();
                 return;
             }
            thandler.postDelayed(this, 243); //looping this for every 243 ms
             }
        };

you have to add return inside the if condition or use the if-else construct

Upvotes: 1

Related Questions