Reputation: 304
I have read the other posts about stopping a Runnnable; I'm pretty new at this.
I am trying to get a Custom View to draw a rectangle, wait a bit, and draw another one. To try to figure out how Runnables work, I'm reading all I can and using Toast to see how many times actions are being repeated.
I've gotten a simple one to work if I stop it by doing something like clicking a button or sliding a slider. HOwever, I want it to stop by itself.
h = new Handler();
// assorted details -- I'm using a Slider for user input.
public void onStopTrackingTouch (SeekBar firstSlider){
if(((addend1 > 0) && (addend2<0))||(addend1<0&&addend2>0) )
{
h.post(shrink);
}
}
});
private Runnable shrink = new Runnable(){
@Override
public void run() {
for (int j = 0; j < 4; j++){
Toast.makeText(getApplicationContext(), "Subtract!", Toast.LENGTH_SHORT).show();
tempadd++;
Log.i("WatchAnimActivity", "tempadd is " + tempadd);
checkTemp();
h.postDelayed(shrink, 18000);
}
}};
public void checkTemp()
{
if (tempadd >= 2)
Log.i("WatchAnimActivity", "tempadd in Check Temp is " + tempadd);
h.removeCallbacks(shrink);}
}
I also tried this:
public void checkTemp( { h.removeCallbacks(shrink); }
I'm having trouble understanding why Android seems to be doing "checkTemp" but ... won't remove the callbacks. I set up my second SLider to h.removeCallbacks(shrink) when it stopped, and that works fine. Why does it fail to h.removeCallbacks even though it's acknowledging that tempadd is 'way greater than two?
Hoping for a lame syntax newbie error here...
Upvotes: 1
Views: 112
Reputation: 15775
It's because you are calling checkTemp()
and remove the callbacks, then immediately post the shrink object again when checkTemp()
returns.
Upvotes: 1