Second article of the new series about Android Fundamentals in which I’d like to touch on some grounds of Android operating system.
First article – Android Fundamentals: Magic of threads – Loopers
1. Big picture
Imagine that user touches the screen of his phone few times in a row. All these events must be handled so we need some kind of a data structure to store these instructions. As it turns out, we have a queue inside every Looper. But how these events get enqueued?
2. Communication between threads
Intuition: Handler is a bridge between threads
These actions are enqueued thanks to the Handlers which are responsible for handling messages. The whole process is as follows:
- We send a message from a background thread to the Main Thread
2. Handler receives a message from a background thread and passes our message to the Message Queue which is located in the Looper
3. Once previous message has been finally processed then our message is sent back to the Handler
4. Then message is sent to the Activity on the Main Thread (actually CustomHandler also works on the Main Thread)
Finally, our message is delivered!