What's the best way to defer a bootloader driven request?

I am using LoaderManager to handle a listview backed request. It relies on some parameters that are unknown up to this point. I am currently calling

getSupportLoaderManager().initLoader(0, null, callback);

      

inside onCreate (). This will immediately create the loader and execute the request before all parameters are known. To handle this, I have a dummy request in onCreateLoader () where not all parameters are known, and a flag in my callback handler that checks inside onLoadFinished () to see if we have a legitimate request. If not, I ignore the cursor data. When the time comes to execute a real request, I call

getSupportLoaderManager().restartLoader(0, null, callback);

      

and also set my flag to true so that onLoadFinshed () handles it correctly.

What I am doing above looks like hacking; Is there a better way? I originally tried to call initLoader () later when I need the request first, but then the situation doesn't change when the orientation changes, similar to this question .

+3


source to share


4 answers


To close the question, for my use case, I could rely on using loader managers for each chunk, instantiating those when I needed them and had the information. If that doesn't suit anyone else, perhaps my original hack is good enough if you want to avoid writing AsyncTasks and take advantage of some of the built-in bootloader functionality.



0


source


You can just call restartLoader

without calling initLoader

. Just remove the call initLoader

from onCreate

and run restartLoader

after you have your parameters, no flag.



0


source


I found another, much simpler solution. For some reason, you have to call initLoader () in your onCreate (), but since you need to pass LoaderCallbacks every time you call restartLoader (), it doesn't really matter which callbacks you use in that first, dummy call initLoader (). I have created very simple utilities:

public static void initEmptyLoaders(Activity activity, int... ids) {
    initEmptyLoaders(activity, activity.getLoaderManager(), ids);
}

public static void initEmptyLoaders(final Context ctx, LoaderManager loaderManager, int... ids) {
    LoaderManager.LoaderCallbacks callbacks = new LoaderManager.LoaderCallbacks() {
        @Override
        public Loader onCreateLoader(int i, Bundle bundle) {
            return new Loader(ctx);
        }

        @Override
        public void onLoadFinished(Loader loader, Object o) {
        }

        @Override
        public void onLoaderReset(Loader loader) {
        }
    };

    for (int id : ids) {
        loaderManager.initLoader(id, null, callbacks);
    }
}

      

Now I call initEmptyLoaders () in my onCreate () with every loader ID I might need later - everything works :-)

0


source


This is an old problem that still exists in some androids, and the support libraries are more updated. Then use android.support.v4.*

the loader API version to fix it.

0


source







All Articles