Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Must I use Fragments to get a LoaderManager?

According to some of the documentation I can use a Loader in either an Activity or a Fragment but the Compatibility Package is showing that only a FragmentActivity has the getLoaderManager() method.

Since the old managed cursor has been deprecated we have to use the Cursor Loader. So why should this data management function be tied to a choice of UI widgets ? It makes no sense to me.

thanks P.

like image 823
pierre renoir Avatar asked Sep 30 '11 04:09

pierre renoir


People also ask

What is LoaderManager in android?

Simply stated, the LoaderManager is responsible for managing one or more Loader s associated with an Activity or Fragment . Each Activity and each Fragment has exactly one LoaderManager instance that is in charge of starting, stopping, retaining, restarting, and destroying its Loader s.

What is a cursor loader?

A CursorLoader is a specialized member of Android's loader framework specifically designed to handle cursors. In a typical implementation, a CursorLoader uses a ContentProvider to run a query against a database, then returns the cursor produced from the ContentProvider back to an activity or fragment.

What is the importance of loader in Android programming?

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 async task loader in Android?

Android AsyncTask going to do background operation on background thread and update on main thread. In android we cant directly touch background thread to main thread in android development. asynctask help us to make communication between background thread to main thread.


2 Answers

Since the old managed cursor has been deprecated we have to use the Cursor Loader.

No, managed cursors still work. Usually, "deprecated" in Android means "we will support this mechanism as long as we can, but we think there are better options".

So why should this data management function be tied to a choice of UI widgets ?

It's not. You do not have to use fragments to inherit from FragmentActivity. You are also welcome to create your own LoaderManagerCapableActivity, where you clone the relevant data members and methods from FragmentActivity (the source code is on your hard drive). However, LoaderManager has to be associated with an activity, because it is tied into the activity lifecycle, as are the managed cursors. For example, the LoaderManager instances are passed between activities via onRetainNonConfigurationInstance().

like image 183
CommonsWare Avatar answered Sep 23 '22 08:09

CommonsWare


Yes, inorder to get loaderManager it must be associated with an a activity however it is possible to use loaders without the need of loaderManger.

1) Implement the AsynTaskLoader.
2) Instantiate the Loader you implemented in your class.
3) Register a listener for your loader so that you will get the callback once the load is complete.
4) Call the startLoading method of the loader.
5) After the load is complete the callback method will be called where you can use the loaded data for any purpose.

like image 26
Sunil Avatar answered Sep 24 '22 08:09

Sunil