LoaderManager with multiple loaders: how to get the right cursorloader LoaderManager with multiple loaders: how to get the right cursorloader android android

LoaderManager with multiple loaders: how to get the right cursorloader


The Loader class has a method called getId(). I would hope this returns the id you've associated with the loader.


Use the getId() method of Loader:

@Overridepublic void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {    switch (loader.getId()) {        case 0:            // do some stuff here            break;        case 1:            // do some other stuff here            break;        case 2:            // do some more stuff here            break;        default:            break;    }}    


If your loaders have nothing in common but the class type of the result (here: Cursor), you're better off creating two separate LoaderCallbacks instances (simply as two inner classes in your Activity/Fragment), each one dedicated to one loader treatment, rather than trying to mix apples with oranges.

In your case it seems that both the data source and the result treatment are different, which requires you to write the extra boilerplate code to identify the current scenario and dispatch it to the appropriate code block.