When we try to execute long running operations on the app’s main thread (also called the UI thread), we freeze the app’s user interface and after a couple seconds we get an “application not responding” dialog message with the option to force quit the app. To avoid this we have to move heavy operations like database manipulation and network requests onto a separate thread.
In this video we will learn how to create such a separate worker thread in Android. For this we can either create a class that extends Thread and override it’s run method, or create a class that implements the Runnable interface, and then pass this Runnable to a new Thread object.
These core Java classes build the foundation for classes like AsyncTask, HandlerThread and ThreadPoolExecutor.
We will also learn how to send code from the background thread back to the UI thread by calling post on a Handler that sends a runnable to the MessageQueue of the UI thread. To associate the Handler with the Looper of the main thread, we either instantiate it on the main thread, or pass Looper.getMainLooper to the constructor.
Instead of creating a handler, we can also use the View classes post method or the Activity classes runOnUiThread method, that use the main handler internally.
Great explainer video by the Android developer team about threads, handlers, loopers etc:
Watch this playlist to learn more about Looper, MessageQueue & Handler:
❗ Subscribe to the channel:
⏯ Watch more tutorials:
㊙ Help translating the videos:
💚 Support the content:
💬 Join the Discord chat:
🎧 Audiobooks and podcasts for programmers:
👶 How to get started with Android development:
📣 Follow Coding in Flow on social media: