Removecallbacks Not Stopping Runnable

removeCallbacks not stopping runnable

It appears to me that removeCallbacks(..) only stops pending messages (Runnables). If your runnable has already started, then there's no stopping it (at least not this way).

Alternatively, you can extend the Runnable class and give it some kind of kill switch like this:

public class MyRunnable implements Runnable
{
private boolean killMe = false;

private void run()
{
if(killMe)
return;

/* do your work */
}

private void killRunnable()
{
killMe = true;
}
}

This will only prevent it from starting, but you could occasionally check killMe and bail out. If you are looping the runnable (like some kind of background thread) you can say:

while(!killMe) {
/* do work */
}

Hope this helps

EDIT I just wanted to post an update on this. Since this original post, Google has come up with a great class called AsyncTask that handles all of this stuff for you. Anyone reading this really should look into it because it is the correct way of doing things.

You can read about it here

Android Runnable not stopping after removeCallbacks()

boolean stoped = false;

Runnable mStatusChecker = new Runnable() {
@Override
public void run() {
try {
checkPayNow();
} finally {
if(!stoped)
mHandler.postDelayed(mStatusChecker, mInterval);
}
}
};

Make stoped = true when you want to stop.

and remove handler from checkPayNow().

public void checkPayNow(){

if (!url.isEmpty()){
//url now has text
//mHandler.removeCallbacks(mStatusChecker);
}else {
//no text yet
}
}

handler.removeCallbacks(runnable) does not stop the handler when call

It seems the expected behavior since you are calling removeCallback from latAndLong method but you call again postDelayed in the run. Return a boolean value from latAndLong method and decide if you need to call postDelayed again or not in the Run method.

Unable to stop handler despite calling removeCallbacks(runnable)

I am using below code snippet to run a thread every 10 seconds and update UI based on the values received from Server, stop thread on onPause.

Declare below variables in your class:

public class ActivityPortfolio extends AppCompatActivity {
Handler handler = new Handler();
Runnable runnable;
int delay = 10*1000;
//---------

In your onResume:

@Override
protected void onResume() {

handler.postDelayed(new Runnable(){
public void run(){

doSomething();
handler.postDelayed(this, delay);
}
}, delay);

super.onResume();
}

onPause():

  @Override
protected void onPause() {
super.onPause();
handler.removeCallbacks(runnable); // this alone didnt work as we are calling postDelayed() in background as well.
handler.removeCallbacksAndMessages(null);//after adding this it stops thread
}

removeCallbacks() doesn't seems to delete the specified Runnable in Handler

handler.removeCallbacksAndMessages(null);

In the docs for removeCallbacksAndMessages it says...

Remove any pending posts of callbacks and sent messages whose obj is token. If a token is null, all callbacks and messages will be removed.

removeCallbacks only stops pending messages (Runnables). If your runnable has already started, then there's no stopping it (at least not this way).

Alternatively, you can extend the Runnable class and give it some kind of kill switch like this:

public class MyRunnable implements Runnable
{
public boolean killMe = false;

public void run()
{
if(killMe)
return;

/* do your work */
}

public void killRunnable()
{
killMe = true;
}
}

This will only prevent it from starting, but you could occasionally check killMe and bail out. If you are looping the runnable (like some kind of background thread) you can say:

while(!killMe) {
/* do work */
}

Remove callback not working in Handler

The problem is that myFunction removes the callback, then you still call handler.postDelayed to schedule a new one. There are plenty of ways to refactor this. For example:

handler=new Handler();
handler.post(runnable);
public Runnable runnable = new Runnable() {

@Override
public void run() {
boolean reschedule = myFunction(position);
if(reschedule) {
handler.postDelayed(runnable,10000);
}
}
};
public boolean myFunction(int position)
{
if(position>10) {
return false;
}
return true;
}

You don't have to remove callbacks on the handler because a new one will not be scheduled in the first place.

I can not stop a Runnable using Handler in JAVA

Please remove all conditions blocks where you are checking start_button.isPressed(), it doesn't work. You can use additional flag isRunning to indicate whether the clock is running or not:

private boolean isRunning;

start_button.setOnClickListener(view -> {
if (isRunning) {
newHandler.removeCallbacks(updateTimerThread);
start_button.setText("Start");
} else {
start_time = SystemClock.uptimeMillis();
start_button.setText("Stop");
newHandler.post(updateTimerThread);
}
isRunning = !isRunning;
});

Android Stopping Runnable onBackPressed()

Finally got this to work. The NPE was due to not initializing the handler class wide. After I fixed that the runnable would still not stop with handler.removeCallbacks(runnable); What seems to be working is:

handler.removeCallbacksAndMessages(null);.



Related Topics



Leave a reply



Submit