Search code examples
javaandroidhandlerrunnable

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?


Solution

  •         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