I am aware this is most likely a very stupid question, but I just cannot completely understand the point of Handlers. I know the idea is that Handlers execute on the main thread, and they seem to be most commonly used along with worker threads, but why cannot the worker thread just invoke methods on the calling Activity instead of said Activity creating the thread along with a Handler to receive messages?
Again, I apologise for the ignorance of my question, but all I’ve found online is tutorials on how to use Handlers and my Pro Android 3 book does not clear up my question (or I’m too stupid to understand it properly!)
Or, for that matter, why use them over AsyncTasks, which can modify the UI without problem?
Thank you.
When using handler (or message) each task becomes “serialized”.
This has the advantage that there is NO concurrency and therefore no need to lock.
It is much easier to make a message driven system stable than an multi-threaded one.
By the way AsyncTask uses Handlers, too
Using handlers directly give you more flexibility.
For example you could schedule a message to be send in the future.
Or you could abort an scheduled message.
Handlers are a very powerful tool.