Loaders in Android Honeycomb
Asked Answered
M

3

36

I'm trying to figure out how to use Loaders in Android 3.0 but can't seem to get it to work. The docs only describe using CursorLoader but I'm using AsyncTaskLoader.

From the docs it seems that you should only need to implement AsyncTaskLoader.loadInBackground() but it never gets called after getLoaderManager().initLoader() and then creating the loader in the callback.

I can see debug messages saying Created new loader LoaderInfo{4040a828 #0 : ArticleDataLoader{4036b350}} so it seems like it is created successfully.

Is it possible that loaders are currently broken in the SDK or is there some method you need to call after creating the loader? (they haven't done that in the CursorLoader example).

EDIT: Seems like calling forceLoad() on the Loader returned from initLoader() starts the loading at least but this means you can't handle rotations correctly :(

Melbourne answered 7/2, 2011 at 11:45 Comment(4)
If you find an answer to this please let me know as well. I haven't been able to find anything.Opia
There's also code.google.com/p/android/issues/detail?id=14944 which mentions the same workaround as the 'Edit' comment.Unorthodox
Yes, that is my bug report about this :)Melbourne
Have you checked developer.android.com/reference/android/app/LoaderManager.html?Lear
M
13

Dianne Hackborn replied on the bug tracker and referred us to the static library implementation. CursorLoader is doing forceLoad() which is why it is working.

See my attached class for a class which handles this for you in most simple cases at the bug tracker: http://code.google.com/p/android/issues/detail?id=14944

Melbourne answered 27/3, 2011 at 10:16 Comment(3)
they really need to document the CPL >.<Nary
That really sucks. So the example shown here: developer.android.com/reference/android/content/… if you use the Support Library won't work unless you override onStartLoadingSummers
So now I have my own reference source for ASyncTaskLoader using the Support Library: blog.blundell-apps.com/…Summers
P
1

You need to override the onStartLoading() method. Look at the example on the developer website.

    /**
     * Handles a request to start the Loader.
     */
    @Override protected void onStartLoading() {
        if (mApps != null) {
            // If we currently have a result available, deliver it
            // immediately.
            deliverResult(mApps);
        }

        // Start watching for changes in the app data.
        if (mPackageObserver == null) {
            mPackageObserver = new PackageIntentReceiver(this);
        }

        // Has something interesting in the configuration changed since we
        // last built the app list?
        boolean configChange = mLastConfig.applyNewConfig(getContext().getResources());

        if (takeContentChanged() || mApps == null || configChange) {
            // If the data has changed since the last time it was loaded
            // or is not currently available, start a load.
            forceLoad();
        }
    }
Pringle answered 23/6, 2012 at 2:48 Comment(0)
S
0

Alex; Did you try to validate if the onLoadInBackground () gets even called?

onLoadInBackground (): Called on a worker thread to perform the actual load. Implementations should not deliver the result directly, but should return them from this method, which will eventually end up calling deliverResult(D) on the UI thread. If implementations need to process the results on the UI thread they may override deliverResult(D) and do so there.

Senna answered 3/3, 2011 at 18:37 Comment(1)
No, it does not get called unless you do forceLoad().Melbourne

© 2022 - 2024 — McMap. All rights reserved.