0

please teach me how to stop my runnable. "handler.removeCallback(sS)" does not work. Or is my program wrong?

int life = 100, damage = 5;
public void gameOver()
{
    ((TextView)findViewById(R.id.textView6)).setVisibility(View.VISIBLE);
}
final Runnable sS = new Runnable()
    {
        @Override
        public void run()
        {
            life = life - damage;
            String lifeHP = String.valueOf(life);
            ((TextView)findViewById(R.id.textView)).setText(lifeHP);
            handler.postDelayed(this, 300);
            if (life < 0 )
            {
                gameOver();
                //handler.removeCallback(ss)--does not work.
            }
        }
    };
    handler.post(sS);
  • Does this answer your question? [How to stop Runnable on button click in Android?](https://stackoverflow.com/questions/16188398/how-to-stop-runnable-on-button-click-in-android) – Gk Mohammad Emon Sep 06 '20 at 03:44
  • hmm...looks when only callback is removed...there is a message in the queue...which when executes assigns the runnable back to handler...I think you need to use removeCallbacksAndMessages here. – Abhiroj Panwar Sep 06 '20 at 03:51

0 Answers0