我有一个问题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;
}
}