How to remove all callbacks from a Handler?
In my experience calling this worked great!
handler.removeCallbacksAndMessages(null);
In the docs for removeCallbacksAndMessages it says...
Remove any pending posts of callbacks and sent messages whose obj is token. If token is
null
, all callbacks and messages will be removed.
Remove callback for handler not work
removeCallbacks(Runnable r):
Remove any pending posts of Runnable r that are in the message queue.
so removeCallbacks(..)
only stops pending messages (Runnables) not currently running runnable so if you want to stop currently running Runable then use a Boolean varaible for Stoping Thread when user Exit from your app.
see this post for removeCallbacks not stopping runnable
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.
How to reuse Android-Handler after removing callbacks
If you want to reuse the handler you have to do:
handler.removeCallbacksAndMessages(null);
In the docs for removeCallbacksAndMessages it says...
Remove any pending posts of callbacks and sent messages whose obj is token. If token is null, all callbacks and messages will be removed.
I hope It helps you!
Stop handler.postDelayed()
You can use:
Handler handler = new Handler()
handler.postDelayed(new Runnable())
Or you can use:
handler.removeCallbacksAndMessages(null);
Docs
public final void removeCallbacksAndMessages (Object token)
Added in API level 1 Remove any pending posts of callbacks and sent
messages whose obj is token. If token is null, all callbacks and
messages will be removed.
Or you could also do like the following:
Handler handler = new Handler()
Runnable myRunnable = new Runnable() {
public void run() {
// do something
}
};
handler.postDelayed(myRunnable,zeit_dauer2);
Then:
handler.removeCallbacks(myRunnable);
Docs
public final void removeCallbacks (Runnable r)
Added in API level 1 Remove any pending posts of Runnable r that are
in the message queue.public final void removeCallbacks (Runnable r, Object token)
Edit:
Change this:
@Override
public void onClick(View v) {
Handler handler = new Handler();
Runnable myRunnable = new Runnable() {
To:
@Override
public void onClick(View v) {
handler = new Handler();
myRunnable = new Runnable() { /* ... */}
Because you have the below. Declared before onCreate
but you re-declared and then initialized it in onClick
leading to a NPE.
Handler handler; // declared before onCreate
Runnable myRunnable;
Android Remove Handler postDelayed Dynamically
mHandler.removeCallbacks(runnable);
will only remove any pending posts of Runnable r that are in the message queue. It will not stop an already running thread. You have to explicitly stop the thread. One way to stop that thread is to use a boolean variable as a flag and run your code inside runnable based on the value of that flag. You can take some hints from https://stackoverflow.com/a/5844433/1320616
How to remove a runnable from a handler object added by postDelayed?
Just use the removeCallbacks(Runnable r)
method.
Related Topics
Mapping Manytomany with Composite Primary Key and Annotation:
How to Initialize List<String> Object in Java
How to Check Available Space on Android Device? on Sd Card
Opengl Extensions Available on Different Android Devices
Code Will Only Return 0.0, 0.0 Gps Coordinate While Throwing Nullpointerexception
Android App in Eclipse: Edit Text Not Showing on Graphical Layout
Android - Copy Assets to Internal Storage
How to Pass Values Between a Dialog and an Activity
How Big Is an Object Reference in Java and Precisely What Information Does It Contain
Is Addition of Byte Converts to Int Because of Java Language Rules or Because of Jvm
Easiest Way to Convert a List to a Set in Java
Getting Jsoup to Support Dynamically Generated HTML by JavaScript
Android Project Using Httpclient --> Http.Client (Apache), Post/Get Method
How to Download Older Google Play Services
Sort Data to Recyclerview Based on Latest Date from Firebase