Tuesday, December 31, 2013

Android Loaders

Loaders in Android

Introduction with Honeycomb
Where we can use?
1) to access data of databases 2) content providers

They load data asynchronously and notify the listeners when the results are available/ready.

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. Thus, they don't need to re-query their data.


the main classes and interfaces are as folllows


1)LoaderManager = Manages your Loaders for you. Responsible for dealing with the Activity or Fragment lifecycle


2)Loader =The base class for all Loaders


3)LoaderManager.LoaderCallbacks = A callback interface you must implement


4)AsyncTaskLoader = An implementation that uses an AsynTask to do its work


5)CursorLoader = A subclass of AsyncTaskLoader for accessing ContentProvider data


eXample


please visit at github




and


https://github.com/emil10001/AndroidSerialSQL/tree/sample (Source code)

Advantage==>  Loaders in Android are great, they allow us to asynchronously load data to be used in Adapters. It is found that CursorLoaders very useful for getting data from a database to the UI in a way that minimizes blocking calls on the UI thread.

Google did not only introduce Loaders but also deprecated the previous way to handle a Cursor within your activities. You shouldn’t use startManagingCursor()or managedQuery() in your projects anymore.