The loaders
help in asynchronously getting the data from a data source. We can achieve the same effect using Handlers
, where we can kick off a thread or execute a Executor and can fetch the data in that thread. Once data is fetched, we can update the UI using the UI handler Message mechanism.
Then why do go for coding the complex loaders when we can achieve the same by using Handlers
.
Loaders persist and cache results across configuration changes to prevent duplicate queries. Loaders can implement an observer to monitor for changes in the underlying data source. For example, CursorLoader automatically registers a ContentObserver to trigger a reload when data changes.
Using Handlers you have the advantage of MessagingQueues , so if you want to schedule messages or update multiple UI elements or have repeating tasks. AsyncTask are similar, in fact, they make use of Handler , but doesn't run in the UI thread, so it's good for fetching data, for instance fetching web services.
A Handler allows you to send and process Message and Runnable objects associated with a thread's MessageQueue . Each Handler instance is associated with a single thread and that thread's message queue. When you create a new Handler it is bound to a Looper .
This is where Handler comes in a. It always attaches itself to the Looper of the thread, on which its instance is created. b. It then processes those messages of the thread it attaches to.
Introduced in Android 3.0, loaders make it easy to asynchronously load data in an activity or fragment. Loaders have these characteristics: They are available to every Activity and Fragment. They provide asynchronous loading of data. They monitor the source of their data and deliver new results when the content changes.
Looper and Handler are one of the key low-level components of Android. For example, the UI thread is built with them. However, only a few developers use them directly nowadays. In this article, we’ll try to understand how they work. The Looper class is essentially an event loop for a thread. There can be at most one Looper per thread.
Handler is the most precious thing on android framework. Yes it is not a java thing. Before knowing about handler we have to know about two things Runnable and Message. What is a Handler? In simple words, Handler is not a thread and Handler is not related to the UI.
Loaders have these characteristics: They are available to every Activity and Fragment. They provide asynchronous loading of data. They monitor the source of their data and deliver new results when the content changes. They automatically reconnect to the last loader's cursor when being recreated after a configuration change.
Loaders were introduced to make it easier in implementing correct data loading on android platform. This means:
using Handlers,Executors, or AsyncTasks does not take into account all above points. Your will have to manage this by yourself, and this is the work android developers put into Loaders implementation.
Ie. using AsyncTask for loading some data requires you to watch out for screen rotations, ie. you must somehow retain reference to your AsyncTask that might still do something in background once you activity is recreated due to screen rotation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With