【问题标题】:Android Custom Loader, LoaderManagerImpl.LoaderInfo.callOnLoadFinished only called onceAndroid 自定义 Loader,LoaderManagerImpl.LoaderInfo.callOnLoadFinished 只调用一次
【发布时间】:2013-10-27 13:40:21
【问题描述】:

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

我首先考虑从 AsyncLoader 子类化,但我并不真的需要将数据加载放在 AsyncTask 上(这就是 AsyncLoader 在后台所做的)。在我的案例中,基础数据是/是来自本机库的数据/样本。这些示例在库中兑现,这与我的应用程序完全异步,因此无需在单独的线程中迭代此本机缓存。

我的自定义 Loader 大致如下所示:

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

我正在使用 Fragment 通过 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 跟踪:

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 => onLoadComplete [=> callOnLoadFinished => 片段更新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 != data(因为在这种情况下为 mData == null)。在随后遇到这种情况时,mData == data 总是(并且数据对象/数组随着我的本机输入正确增长),这很奇怪,因为我无法找出是谁在设置/更新它LoaderManagerImpl 中的 LoaderInfo 类中的 mData 对象。

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

任何人都知道我在自定义加载器中做错了什么,或者当底层数据集一直在变化时,自定义加载器不是一个很好的解决方案?

提前致谢! 巴特

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

【问题讨论】:

  • 感谢您的提问!我已经挣扎了半天了!不知道它会比较底层的对象实例。

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


【解决方案1】:

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

【讨论】:

  • pskink,这确实解决了问题!我想 clear 只是清除 ArrayList 中对单个 TestSample 对象的引用。因此,从 ArrayList 的角度来看(也就是 LoaderManager),数组并没有真正改变(大小是相同的,只有引用是空的)。伟大的!再次感谢您的解决方案!
猜你喜欢
  • 2012-03-03
  • 1970-01-01
  • 2011-07-01
  • 2015-08-20
  • 1970-01-01
  • 1970-01-01
  • 2021-10-01
  • 1970-01-01
  • 2021-10-09
相关资源
最近更新 更多