How to create a Looper thread, then send it a message immediately?

Eventual solution (minus error checking), thanks to CommonsWare: class Worker extends HandlerThread { // … public synchronized void waitUntilReady() { d_handler = new Handler(getLooper(), d_messageHandler); } } And from the main thread: Worker worker = new Worker(); worker.start(); worker.waitUntilReady(); // <- ADDED worker.handler.sendMessage(…); This works thanks to the semantics of HandlerThread.getLooper() which blocks until the … Read more

Completion handler for UINavigationController “pushViewController:animated”?

See par’s answer for another and more up to date solution UINavigationController animations are run with CoreAnimation, so it would make sense to encapsulate the code within CATransaction and thus set a completion block. Swift: For swift I suggest creating an extension as such extension UINavigationController { public func pushViewController(viewController: UIViewController, animated: Bool, completion: @escaping … Read more

Does jQuery have a handleout for .delegate(‘hover’)?

User113716’s great answer will no longer work in jQuery 1.9+, because the pseudo-event hover is no longer supported (upgrade guide). Also since jQuery 3.0 delegate() for binding events is officially deprecated, so please use the new on()(docs) for all event binding purposes. You can easily migrate user113716‘s solution by replacing hover with mouseenter mouseleave and … Read more

Handlers and memory leaks in Android

I recently updated something similar in my own code. I just made the anonymous Handler class a protected inner class and the Lint warning went away. See if something like the below code will work for you: public class MyGridFragment extends Fragment{ static class MyInnerHandler extends Handler{ WeakReference<MyGridFragment> mFrag; MyInnerHandler(MyGridFragment aFragment) { mFrag = new … Read more

What’s the difference between Event Listeners & Handlers in Java?

There’s no formally defined difference between listeners and handlers. Some people would probably argue that they are interchangeable. To me however, they have slightly different meaning. A listener is an object that subscribes for events from a source. Cf. the observer pattern. Usually you can have many listeners subscribing for each type of event, and … Read more

TimerTask vs Thread.sleep vs Handler postDelayed – most accurate to call function every N milliseconds?

There are some disadvantages of using Timer It creates only single thread to execute the tasks and if a task takes too long to run, other tasks suffer. It does not handle exceptions thrown by tasks and thread just terminates, which affects other scheduled tasks and they are never run ScheduledThreadPoolExecutor deals properly with all … Read more

What is the relationship between Looper, Handler and MessageQueue in Android?

A Looper is a message handling loop: it reads and processes items from a MessageQueue. The Looper class is usually used in conjunction with a HandlerThread (a subclass of Thread). A Handler is a utility class that facilitates interacting with a Looper—mainly by posting messages and Runnable objects to the thread’s MessageQueue. When a Handler … Read more

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 … Read more

Where do I create and use ScheduledThreadPoolExecutor, TimerTask, or Handler?

I prefer to use ScheduledThreadPoolExecutor. Generally, if I understand your requirements correctly, all these can be implemented in your activity, TimerTask and Handler are not needed, see sample code below: public class MyActivity extends Activity { private ScheduledExecutorService scheduleTaskExecutor; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); scheduleTaskExecutor= Executors.newScheduledThreadPool(5); // This schedule a task to run every … Read more

tech