AsyncTaskLoader doesn't call onLoadFinished

本秂侑毒 提交于 2019-12-10 12:26:01

问题


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?

Here's simplified 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();
    }
}

and 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;
     }
}

回答1:


The problem is solved - Sherlock Fragment should use SupportLoaderManager .



来源:https://stackoverflow.com/questions/24564816/asynctaskloader-doesnt-call-onloadfinished

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!