[Loader] 从startLoading()说开去..


上一篇主要理了下LoaderCallback, 其中看到LoaderManager在initLoader之后自动调用了Loader的startLoading方法; 这一篇继续理Loader !最后我们就可以把Loader执行过程的整个生命周期画出来了。


先看结构吧,Loader简单多了。

[Loader] 从startLoading()说开去..

该类源码查看:http://www.oschina.net/code/explore/android-4.0.1/core/java/android/content/Loader.java


首先是startLoading 方法

    public final void startLoading() {
        mStarted = true;
        mReset = false;
        mAbandoned = false;
        onStartLoading();
    }

    /***
     * Subclasses must implement this to take care of loading their data,
     * as per {@link #startLoading()}.  This is not called by clients directly,
     * but as a result of a call to {@link #startLoading()}.
     */
    protected void onStartLoading() {
    }

看到没,设置了几个状态, 然后调用onStartLoading方法就结束了, 而onStartLoading方法又是个空方法.. 好了,圆满结束!....


尼玛, 难怪我的loadInBackground方法总是不被调用! 坑爹啊! 那怎么让你自己Loader的loadInBackground正常被调用呢? 只能重写onStartLoading方法呗!

看看官方的示例(http://developer.android.com/intl/zh-CN/reference/android/content/AsyncTaskLoader.html):

    /**
     * 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();
        }
    }

有事.. 先到这..



你可能感兴趣的:(loader)