[LoaderManager]从getLoaderManager().initLoader(0, null, this);说开去

首先一起看一下整个LoaderManager类文件, 尼玛..

[LoaderManager]从getLoaderManager().initLoader(0, null, this);说开去

源码:http://www.oschina.net/code/explore/android-4.0.1/core/java/android/app/LoaderManager.java


先来看下getLoaderManager方法:

    public LoaderManager getLoaderManager() {

        if (mLoaderManager != null) {
            return mLoaderManager;
        }
        mCheckedForLoaderManager = true;
        mLoaderManager = getLoaderManager(-1, mLoadersStarted, true);
        return mLoaderManager;
    }

    LoaderManagerImpl getLoaderManager(int index, boolean started, boolean create) {
        if (mAllLoaderManagers == null) {
            mAllLoaderManagers = new SparseArray<LoaderManagerImpl>();
        }
        LoaderManagerImpl lm = mAllLoaderManagers.get(index);
        if (lm == null) {
            if (create) {
                lm = new LoaderManagerImpl(this, started);
                mAllLoaderManagers.put(index, lm);
            }
        } else {
            lm.updateActivity(this);
        }
        return lm;
    }

可以很清晰的看到,实际上是获取了一个LoaderManagerImpl实例。很自然,接下来应该看下initLoader()方法了

final SparseArray<LoaderInfo> mLoaders = new SparseArray<LoaderInfo>();

    @SuppressWarnings("unchecked")
    public <D> Loader<D> initLoader(int id, Bundle args, LoaderManager.LoaderCallbacks<D> callback) {
        if (mCreatingLoader) {
            throw new IllegalStateException("Called while creating a loader");
        }
         
        LoaderInfo info = mLoaders.get(id);
         
        if (DEBUG) Log.v(TAG, "initLoader in " + this + ": args=" + args);
 
        if (info == null) {
            // Loader doesn't already exist; create.
            info = createAndInstallLoader(id, args,  (LoaderManager.LoaderCallbacks<Object>)callback);
            if (DEBUG) Log.v(TAG, "  Created new loader " + info);
        } else {
            if (DEBUG) Log.v(TAG, "  Re-using existing loader " + info);
            info.mCallbacks = (LoaderManager.LoaderCallbacks<Object>)callback;
        }
         
        if (info.mHaveData && mStarted) {
            // If the loader has already generated its data, report it now.
            info.callOnLoadFinished(info.mLoader, info.mData);
        }
         
        return (Loader<D>)info.mLoader;
    }

一切的一起从这个方法开始了。

从代码中可以看到,它会先到mLoaders中查找这个Loader,如果该id的Loader还不存在,那就执行createAndInstallLoader方法创建;

    private LoaderInfo createLoader(int id, Bundle args,
            LoaderManager.LoaderCallbacks<Object> callback) {
        LoaderInfo info = new LoaderInfo(id, args,  (LoaderManager.LoaderCallbacks<Object>)callback);
        Loader<Object> loader = callback.onCreateLoader(id, args);
        info.mLoader = (Loader<Object>)loader;
        return info;
    }
    
    private LoaderInfo createAndInstallLoader(int id, Bundle args,
            LoaderManager.LoaderCallbacks<Object> callback) {
        try {
            mCreatingLoader = true;
            LoaderInfo info = createLoader(id, args, callback);
            installLoader(info);
            return info;
        } finally {
            mCreatingLoader = false;
        }
    }
    
    void installLoader(LoaderInfo info) {
        mLoaders.put(info.mId, info);
        if (mStarted) {
            // The activity will start all existing loaders in it's onStart(),
            // so only start them here if we're past that point of the activitiy's
            // life cycle
            info.start();
        }
    }

可以看到,createAndInstallLoader方法又去调用了createLoader 和installLoader方法。createLoader 执行回调onCreateLoader以获取你的Loader实例,而installLoader会执行startLoading方法,以启动你的Loader。


注意,此处回调了LoaderCallBack的onCreateLoader方法!!


如果已经存在,就把callback参数传给LoaderInfo对象;

接下来调用LoaderInfo的callOnLoadFinished方法!

        void callOnLoadFinished(Loader<Object> loader, Object data) {
            if (mCallbacks != null) {
                String lastBecause = null;
                if (mActivity != null) {
                    lastBecause = mActivity.mFragments.mNoTransactionsBecause;
                    mActivity.mFragments.mNoTransactionsBecause = "onLoadFinished";
                }
                try {
                    if (DEBUG) Log.v(TAG, "  onLoadFinished in " + loader + ": "
                            + loader.dataToString(data));
                    mCallbacks.onLoadFinished(loader, data);
                } finally {
                    if (mActivity != null) {
                        mActivity.mFragments.mNoTransactionsBecause = lastBecause;
                    }
                }
                mDeliveredData = true;
            }
        }

注意,此处回调了LoaderCallBack的onLoadFinished方法!!


那还有个onLoaderReset 方法, 什么时候调用呢? 可以明确的告诉你,这个方法只有在Loader被销毁的时候才会调用。

		getLoaderManager().destroyLoader(int)
		getLoaderManager().restartLoader(int arg0, Bundle arg1, LoaderCallbacks<D> arg2)

你可能感兴趣的:(manager)