我目前正在将我的应用程序移植到 android 兼容包(主要用于 CursorLoader 和 Fragments)。我目前正在尝试在两个片段之间共享一个 CursorLoader,以便对我的 ContentProvider 进行查询。欢迎来到我的世界!;)
一个简单的用例:
− DummyActivity 扩展 FragmentActivity / Log.d(Constants.LOGTAG, "DummyActivity.onCreate " + getSupportLoaderManager().toString());
− DataFragment 扩展 Fragment 实现 LoaderManager.LoaderCallbacks / Log.d(Constants.LOGTAG, "DataFragment.onCreate " + getLoaderManager().toString());
− ReportFragment 扩展 Fragment 实现 LoaderManager.LoaderCallbacks / Log.d(Constants.LOGTAG, "ReportFragment.onCreate " + getLoaderManager().toString());
DummyActivity 实例化 DataFragment,后者实例化 ReportFragment。logcat 输出显示每个 LoaderManager 的不同地址。作为第一个结论,每个 Fragment 似乎都有一个合适的 LoaderManager……</p>
如果我能回答你的(我们的;))问题,我会继续更新。如果您取得了任何进展,请分享您的宝贵知识。
更新:
我的假设是加载器 id 仅与特定片段的 LoaderManager 的本地范围相关联,以使多个本地加载器与片段相关联(因此您可以根据 id int arg 在 onCreateLoader 中返回不同的加载器和initLoader 调用)。
到目前为止,我设法“重用”了一个加载器(......或不):
− 首先,我getSupportLoaderManager().enableDebugLogging(true);
在 DummyActivityonCreate
方法中启用了 LoaderManager 调试。
− 然后我getActivity().getSupportLoaderManager().initLoader(78, null, this);
从onCreate
DataFragment 和 ReportFragment 的方法中调用。
onCreateLoader
− DataFragment通过 mCursorLoader 私有成员上的 setter公开该方法创建的 CursorLoader 。
− ReportFragmentonCreateLoader
返回 DataFragment CursorLoader(使用 检索 Fragment 后findFragmentByTag
)。
过滤(并且稍微混淆)的 logcat 输出:
DummyApp D DummyActivity.onCreate
DummyApp D DataFragment.newInstance
DummyApp D ReportFragment.newInstance
DummyApp D DataFragment.onCreate
LoaderManager V initLoader in LoaderManager{405a19d0 in SpecificAction{4059ee98}}: args=null
DummyApp D DataFragment.onCreateLoader
LoaderManager V Created new loader LoaderInfo{405a2298 #78 : CursorLoader{405a22e0}}
DummyApp D DataFragment.onCreate
DummyApp D DataFragment.onActivityCreated
DummyApp D ReportFragment.onCreate
LoaderManager V initLoader in LoaderManager{405a19d0 in DummyActivity{4059ee98}}: args=null
LoaderManager V Re-using existing loader LoaderInfo{405a2298 #78 : CursorLoader{405a22e0}}
DummyApp D SpecificActionReportFragment.onCreate
DummyApp D SpecificActionReportFragment.onActivityCreated
LoaderManager V Starting in LoaderManager{405a19d0 in DummyActivity{4059ee98}}
LoaderManager V Starting: LoaderInfo{405a2298 #78 : CursorLoader{405a22e0}}
DummyProvider D query called
DummyProvider D […]
DummyProvider D [end of query]
LoaderManager V onLoadComplete: LoaderInfo{405a2298 #78 : CursorLoader{405a22e0}}
LoaderManager V onLoadFinished in CursorLoader{405a22e0 id=78}: CursorWrapperInner{405afb20}
DummyApp D ReportFragment.onLoadFinished
DummyApp D ReportFragment.displayActionReport
DummyApp D DummyActivity.setReportViewsVisibility
DummyApp D ReportFragment.setSaveReportImageViewVisibility
这两个片段是从 DummyActivityonCreate
方法添加的(与所描述的用例不同,但这对我们正在处理的问题没有任何改变)。不幸的是,加载器被重新分配给调用它的最新片段(此处为 ReportFragment)……并且永远不会调用 DataFragment.onLoadFinished。因此,ReportFragment 看起来不错,但 DataFragment 不是最新的,因为从onLoadFinished
此类调用更新。
我假设有一个底层取消注册调用,然后是 CursorLoader 上的一个注册调用。
待续……</p>