Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android loaders vs Handlers

Tags:

java

android

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.

like image 571
Sumit Trehan Avatar asked Jun 29 '15 18:06

Sumit Trehan


People also ask

What are Android loaders?

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.

What is the difference between handler and AsyncTask in Android?

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.

What are handlers in Android?

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 .

Does handler create new thread?

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.

What are loaders in Android?

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.

What is Looper and handler in Android?

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.

What is a handler in Android?

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.

What are the characteristics of an loader?

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.


1 Answers

Loaders were introduced to make it easier in implementing correct data loading on android platform. This means:

  • doing all the heavy stuff on background thread
  • safely introducing loaded data in UI
  • caching of data, this means improving speed
  • loaders can live outside the Activity lifecycle, so if you have config change then your data is not destroyed
  • loaders will be reloaded once your data store changes

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.

like image 87
marcinj Avatar answered Sep 27 '22 17:09

marcinj