I am using the LoaderManager to handle a query that is backing a listview. It relies on some parameters that aren’t known until later in the process though. Currently I call
getSupportLoaderManager().initLoader(0, null, callback);
within onCreate(). This will immediately create the loader and execute the query, before the parameters are all known. To handle that, I have a dummy query in onCreateLoader() when not all params are known, and a flag in my callback handler that checks within onLoadFinished() whether or not we have a legitimate query. If it’s not, I ignore the cursor data. When it comes time to do a real query, I call
getSupportLoaderManager().restartLoader(0, null, callback);
and also set my flag to true, so that onLoadFinshed() handles it properly.
What I’m doing above seems kind of hacky; is there a better way? I originally tried to call initLoader() later, when I first need the query, but then things fail upon orientation changes, similar to this question.
Just to close the question, for my use case I was able to rely on using the per-fragment loader managers, instantiating those when I needed them and had the info. If this doesn’t fit anyone else’s use case, perhaps my original hack is good enough, if you want to avoid writing AsyncTasks and take advantage of some of the built-in functionality of a loader.