本文介绍了AsyncTaskLoader不调用onLoadFinished的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对AsyncTaskLoader有问题.当应用启动时,它可以正常工作.然后我打电话给onRefreshStarted,一切都很好.但是,如果更改方向,AsyncTaskLoader开始处理loadInBackground,但从不调用onLoadFinished.怎么了?

I have a problem with AsyncTaskLoader. When app is starting, it works fine. Then I call onRefreshStarted, and again all is good. But if change orientation, AsyncTaskLoader start processing loadInBackground, but onLoadFinished is never called. What is wrong?

以下是简化的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;
     }
}

推荐答案

问题已解决-Sherlock Fragment应该使用SupportLoaderManager.

The problem is solved - Sherlock Fragment should use SupportLoaderManager .

这篇关于AsyncTaskLoader不调用onLoadFinished的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-21 19:51