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


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


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


该类源码查看: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();
        }
    }

有事.. 先到这..



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值