Android Custom Loader,LoaderManagerImpl.LoaderInfo.callOnLoadFinished只调用一次

时间:2013-10-27 13:40:21

标签: android android-fragments android-loadermanager android-loader

我正在尝试实施自己的自定义Android 加载程序,以便能够在我的应用程序中使用 LoaderManager 优势(将加载数据与我的的生命周期解耦活动片段)。

我首先考虑从 AsyncLoader 进行子类化,但我并不需要将数据加载到 AsyncTask (这就是AsyncLoader在引擎盖下做的事情) )。在我的情况下,基础数据是来自本机库的数据/样本。这些示例在库中兑现,这与我的应用程序完全异步,因此无需在单独的线程内迭代此本机缓存。

以下是我的自定义加载程序的内容或多或少:

public class TestSampleListLoader extends Loader<List<TestSample>> {
    private static final String TAG = "TestSampleListLoader";

    private NativeLibFactory mNativeLib = null;
    private SampleReader<TestSample> mTestSampleReader;
    private TestSampleListener mTestSampleSampleListener;
    private List<TestSample> mTestSampleList;

    public TestSampleListLoader(Context context) {
        super(context);
        Log.i(TAG, "TestSampleListLoader constructor!!!");
    }

    @Override
    public void deliverResult(List<TestSample> testSamples) {
        Log.i(TAG, "deliverResult(data) " + testSamples.size());
        super.deliverResult(testSamples);
    }

    @Override
    public boolean isStarted() {
        Log.i(TAG, "isStarted()");
        return super.isStarted();
    }

    @Override
    protected void onStartLoading() {
        Log.i(TAG, "onStartLoading()");
        super.onStartLoading();

        mTestSampleList = new ArrayList<TestSample>();

        if (null == mNativeLib) {
            initNativeLib();
        }
    }

    @Override
    public void forceLoad() {
        Log.i(TAG, "forceLoad()");
        super.forceLoad();
    }

    @Override
    protected void onForceLoad() {
        Log.i(TAG, "onForceLoad()");
        super.onForceLoad();

        mTestSampleList.clear();

        for (TestSample testSample : mTestSampleReader) {
            mTestSampleList.add(testSample);
        }

        Log.i(TAG, "forceLoad(deliverResult) " + mTestSampleList.size());
        deliverResult(mTestSampleList);
    }

    @Override
    protected void onReset() {
        Log.i(TAG, "onReset()");

        mTestSampleList.clear();

        if (null != mTestSampleReader) {
            mTestSampleReader.close();
            mTestSampleReader = null;
        }
        if (null != mNativeLib) {
            mNativeLib.close();
            mNativeLib = null;
        }

        super.onReset();
    }

    @Override
    public void onContentChanged() {
        Log.i(TAG, "onContentChanged()");
        super.onContentChanged();
    }

    private void initNativeLib() {
        Log.i(TAG, "initNativeLib()");
        NativeLibAndroid.initNativeLib(getContext().getApplicationContext(), new NativeLibConnectionListener() {
            @Override
            public void onNativeLibReady(NativeLibFactory NativeLib) {
                Log.d(TAG, "onNativeLibReady!!!");
                mNativeLib = NativeLib;

                mTestSampleSampleListener = new TestSampleListener();
                mTestSampleReader = mNativeLib.createSampleReader(TestSample.class, mTestSampleSampleListener);
            }
        });
    }

    public class TestSampleListener implements SampleReaderListener {
        @Override
        public void onUpdate() {
            Log.i(TAG, "TestSampleListener.onUpdate() => onContentChanged");
            TestSampleListLoader.this.onContentChanged();
        }
    }
}

我使用片段通过 ArrayAdapter 显示我的原生日期样本:

public class TestSampleListFragment extends ListFragment implements LoaderManager.LoaderCallbacks<List<TestSample>> {
    private static final String TAG = "TestSampleListFragment";
    private static final boolean DEBUG = true;

    // The Loader's id (this id is specific to the ListFragment's LoaderManager)
    private static final int LOADER_ID = 1;

    // We use a custom ArrayAdapter to bind application info to the ListView.
    private TestSampleListAdapter mTestReaderAdapter;

    @Override
    public void onActivityCreated(Bundle savedInstanceSample) {
        super.onActivityCreated(savedInstanceSample);
        Log.i(TAG, "onActivityCreated()");

        mTestReaderAdapter = new TestSampleListAdapter(getActivity());
        setEmptyText("No testSamples");
        setListAdapter(mTestReaderAdapter);
        setListShown(false);

        if (DEBUG) {
            Log.i(TAG, "Calling initLoader()!");
            if (getLoaderManager().getLoader(LOADER_ID) == null) {
                Log.i(TAG, "Initializing the new Loader...");
            } else {
                Log.i(TAG, "Reconnecting with existing Loader (id '1')...");
            }
        }

        // Initialize a Loader with id '1'. If the Loader with this id already
        // exists, then the LoaderManager will reuse the existing Loader.
        getLoaderManager().initLoader(LOADER_ID, null, this);
    }

    /**********************/
    /** LOADER CALLBACKS **/
    /**********************/

    @Override
    public Loader<List<TestSample>> onCreateLoader(int id, Bundle args) {
        Log.i(TAG, "onCreateLoader(id) " + id);
        // return new TestSampleListLoader(getActivity());
        TestSampleListLoaderBis testSampleListLoaderBis = new TestSampleListLoaderBis(getActivity());
        return testSampleListLoaderBis;
    }

    @Override
    public void onLoadFinished(Loader<List<TestSample>> loader, List<TestSample> testSampleList) {
        Log.i(TAG, "onLoadFinished(): " + testSampleList.size());
        setListShown(false);
        mTestReaderAdapter.setData(testSampleList);

        if (isResumed()) {
            Log.i(TAG, "onLoadFinished(isResumed)");
            setListShown(true);
        } else {
            Log.i(TAG, "onLoadFinished(isNotResumed)");
            setListShownNoAnimation(true);
        }
    }

    @Override
    public void onLoaderReset(Loader<List<TestSample>> arg0) {
        Log.i(TAG, "onLoaderReset()");
        mTestReaderAdapter.setData(null);
    }
}

ADB Logcat Traces:

D/TestSampleListLoader(31166): onQeoReady!!!
I/TestSampleListLoader(31166): initQeo(mTestSampleReader): org.qeo.internal.SampleReaderImpl@41d29e68

I/TestSampleListLoader(31166): TestSampleListener.onUpdate() => onContentChanged
I/TestSampleListLoader(31166): onContentChanged()
I/TestSampleListLoader(31166): forceLoad()
I/TestSampleListLoader(31166): onForceLoad()
I/TestSampleListLoader(31166): forceLoad(deliverResult) 5
I/TestSampleListLoader(31166): deliverResult(data) 5
I/TestSampleListFragment(31166): onLoadFinished(): 5
I/TestSampleListAdapter(31166): setData(): 5
I/TestSampleListAdapter(31166): setData() for testSample: Test Sample #1
I/TestSampleListAdapter(31166): setData() for testSample: Test Sample #2
I/TestSampleListAdapter(31166): setData() for testSample: Test Sample #3 UPDATED
I/TestSampleListAdapter(31166): setData() for testSample: Test Sample #4
I/TestSampleListAdapter(31166): setData() for testSample: Test Sample #6 UPDATED
I/TestSampleListFragment(31166): onLoadFinished(isResumed)

I/TestSampleListLoader(31166): TestSampleListener.onUpdate() => onContentChanged
I/TestSampleListLoader(31166): onContentChanged()
I/TestSampleListLoader(31166): forceLoad()
I/TestSampleListLoader(31166): onForceLoad()
I/TestSampleListLoader(31166): forceLoad(deliverResult) 5
I/TestSampleListLoader(31166): deliverResult(data) 5

I/TestSampleListLoader(31166): TestSampleListener.onUpdate() => onContentChanged
I/TestSampleListLoader(31166): onContentChanged()
I/TestSampleListLoader(31166): forceLoad()
I/TestSampleListLoader(31166): onForceLoad()
I/TestSampleListLoader(31166): forceLoad(deliverResult) 6
I/TestSampleListLoader(31166): deliverResult(data) 6

问题是,我的 Loader 已正确通知数据更改,但只是第一次将它们传递给 LoaderManager.LoaderCallbacks onLoadFinished( )回调。在方向更改后,它是相同的故事,第一次结果正确到达 onLoadFinished(),但来自本机层的后续更新未到达片段。

我使用eclipse调试功能来追踪问题,我在 LoaderManager 源中找到了它(第447-453行:此代码是从Loader.deliverResult =&gt; onLoadComplete中触发的[= &gt; callOnLoadFinished =&gt;片段更新OK]):

// Notify of the new data so the app can switch out the old data before
// we try to destroy it.
if (mData != data || !mHaveData) {
    mData = data;
    mHaveData = true;
    if (mStarted) {
        callOnLoadFinished(loader, data);
    }
}

似乎只是第一次 mData!=数据(因为 mData == null 在这种情况下)。在此条件的后续命中中, mData == data 总是(并且数据对象/数组正在使用我的本机输入正确增长),这非常奇怪,因为我无法找出谁在设置/更新此 LoaderManagerImpl LoaderInfo 类中的mData对象。

这个问题阻止了我,因为只有当这个条件成立时,才会完成对 callOnLoadFinished 的必要调用,这将正确地通知我的fragment和arrayAdapter有关底层更改。

任何人都知道我在自定义Loader中出错了什么,或者当基础数据集一直在变化时,自定义Loader是不是一个很好的解决方案?

提前致谢! 巴特

Android自定义加载程序的良好参考: http://www.androiddesignpatterns.com/2012/08/implementing-loaders.html

1 个答案:

答案 0 :(得分:1)

如果你想要数据。更改我wolud删除mTestSampleList.clear()并将其替换为mTestSampleList = new ArrayList();