我对AsyncTaskLoader
有疑问。当应用程序启动时,它工作正常。然后我打电话给onRefreshStarted
,而且一切都很好。但是,如果更改方向,AsyncTaskLoader
开始处理loadInBackground
,但永远不会调用onLoadFinished
。有什么问题?
这是简化的SherlockFragment:
public class MyFragment extends SherlockFragment implements LoaderCallbacks<Object> {
PullToRefreshLayout Rl;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (Rl == null) {
Rl = (PullToRefreshLayout) inflater.inflate(
R.layout.friends_fragment_rpc, null);
getData();
} else {
ViewGroup parent = (ViewGroup) Rl.getParent();
parent.removeView(Rl);
getData();
}
setRetainInstance(true);
return Rl;
}
private void getData() {
loaderBndl = new Bundle();
loaderBndl.putString(RequestLoader.ARGS_URL, url);
getActivity().getLoaderManager().initLoader(LOADER_ID, loaderBndl, this)
.forceLoad();
}
@Override
public Loader<Object> onCreateLoader(int id, Bundle args) {
Loader<Object> loader = null;
if (id == LOADER_ID) {
loader = new RequestLoader(getActivity(), args);
}
return loader;
}
@Override
public void onLoadFinished(Loader<Object> loader, Object result) {
Log.d(TAG, "onLoadFinished");
}
@Override
public void onRefreshStarted(View view) {
getData();
}
}
和AsyncTaskLoader:
public class RequestLoader extends AsyncTaskLoader<Object> {
public RequestLoader(Context context, Bundle args) {
super(context);
if (isDebug)
Log.d(TAG, "create RequestLoader");
...
}
@Override
public Object loadInBackground() {
if (isDebug)
Log.d(TAG, "loadInBackground");
Object requestResult = null;
...
return requestResult;
}
}
答案 0 :(得分:1)
问题解决了--Sherlock Fragment应该使用SupportLoaderManager。