我想实现我自己定制的Android 装载机能够在我的应用程序与我生命周期使用LoaderManager好处加载的数据(去耦活动和片段)。的Android定制装载机,LoaderManagerImpl.LoaderInfo.callOnLoadFinished只调用一次
我都首先考虑从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): [email protected]
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
现在的问题是,我装载机正确了解数据的变化,但只有他们第一次交付correclty朝LoaderManager.LoaderCallbacksonLoadFinished()回调。在方向改变后,它是相同的故事,第一次结果正确地到达onLoadFinished(),但从本地层进来的后续更新没有到达片段。
我用蚀调试功能,以跟踪问题,我发现它在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 =数据(因为在这种情况下MDATA == NULL)!在这种情况的后续命中,mData ==数据总是(并且数据对象/数组通过我的本地输入正常增长),这非常奇怪,因为我找不到谁在内设置/更新此mData对象LoaderInfo类LoaderManagerImpl。
这个问题阻止我,因为只有当该条件为真,以必要的呼叫callOnLoadFinished完成,这将正确地告知我片段和arrayAdapter关于潜在的变化。
任何人都知道我在自定义加载程序中做错了什么,或者当底层数据集一直在变化时,定制加载程序不是一个很好的解决方案吗?
在此先感谢! 巴特
在Android定制装载机很好的参考: http://www.androiddesignpatterns.com/2012/08/implementing-loaders.html
谢谢你的问题!我花了半天的时间与它斗争!不知道它在引擎盖下比较对象实例。 –