2

Why use Looper/Handler when I could use an Executor method(s)?

The Looper/Handler duo seems rather clunky and doesn't seem to do a great deal beyond allowing the queuing of runnables and seemingly has less flexibility.

What was the looper design rationale?

Thanks.

  • 2
    `Looper` and `Handler` are mostly there in support of the main application thread. You are welcome to use them for other threads, and occasionally there will be cause for that (see `LocationManager`). However, having used a `HandlerThread` before, a `ThreadPoolExecutor` would be my choice for background work. – CommonsWare Sep 02 '14 at 22:23

1 Answers1

2

A looper by itself does not have many advantages over an executor. It is how Android manages worker. But you are able to get the main thread of your app with Looper.getMainLooper(), this can have the following advantages:

  • You can change the ui from the MainLooper, (only do if not a compute intense background task, since you would freeze the UI)
  • Execute Runnables "in-sync" with the all the other native tasks. Meaning you post your Runnable in the same queue as android does
  • No need to create a thread yourself. You can use the already running Looper.
Patrick
  • 33,984
  • 10
  • 106
  • 126