Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you use a LoaderManager from a Service?

I have a data loading system set up using a custom Loader and Cursor that is working great from Activities and Fragments but there is no LoaderManager (that I can find) in Service. Does anyone know why LoaderManager was excluded from Service? If not is there a way around this?

like image 573
David Sauter Avatar asked Jan 01 '12 22:01

David Sauter


People also ask

What is LoaderManager in android?

This helps an application manage longer-running operations in conjunction with the Activity or Fragment lifecycle; the most common use of this is with a CursorLoader , however applications are free to write their own loaders for loading other types of data. While the LoaderManager API was introduced in Build.

What is 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.


1 Answers

Does anyone know why LoaderManager was excluded from Service?

As stated in the other answer, LoaderManager was explicitly designed to manage Loaders through the lifecycles of Acivities and Fragments. Since Services do not have these configuration changes to deal with, using a LoaderManager isn't necessary.

If not is there a way around this?

Yes, the trick is you don't need to use a LoaderManager, you can just work with your Loader directly, which will handle asynchronously loading your data and monitoring any underlying data changes for you, which is much better than querying your data manually.

First, create, register, and start loading your Loader when your Service is created.

@Override public void onCreate() {     mCursorLoader = new CursorLoader(context, contentUri, projection, selection, selectionArgs, orderBy);     mCursorLoader.registerListener(LOADER_ID_NETWORK, this);     mCursorLoader.startLoading(); } 

Next, implement OnLoadCompleteListener<Cursor> in your Service to handle load callbacks.

@Override public void onLoadComplete(Loader<Cursor> loader, Cursor data) {     // Bind data to UI, etc } 

Lastly, don't forget clean up your Loader when the Service is destroyed.

@Override public void onDestroy() {      // Stop the cursor loader     if (mCursorLoader != null) {         mCursorLoader.unregisterListener(this);         mCursorLoader.cancelLoad();         mCursorLoader.stopLoading();     } } 
like image 145
Steven Byle Avatar answered Sep 21 '22 19:09

Steven Byle