我不会只在实现 LoaderCallbacks 的类中使用私有成员字段,因为您永远不知道究竟哪个加载器正在完成。最好按照提问者的建议进行操作,并将数据与加载程序一起存储。这是我的做法:
public static class CursorWithData<D> extends CursorWrapper {
private final D mData;
public CursorWithData(Cursor cursor, D data) {
super(cursor);
mData = data;
}
public D getData() {
return mData;
}
}
@Override
public Loader<Cursor> onCreateLoader(int id, final Bundle bundle) {
// ...
return new CursorLoader(getActivity(), uri, projection, selection, args, order) {
@Override
public Cursor loadInBackground() {
return new CursorWithData<Bundle>(super.loadInBackground(), bundle);
}
};
}
@Override
public void onLoadFinished(Loader<Cursor> cursorLoader, Cursor cursor) {
CursorWithData<Bundle> cursorWithData = (CursorWithData<Bundle>) cursor;
Bundle args = cursorWithData.getData();
cursor = cursorWithData.getWrappedCursor(); // Optional if you are worried about performance
// ...
}