Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom AsyncTaskLoader, loadinBackground not called after 5 attempts

I have a SherlockListFragment that implements a custom AsyncTaskLoader. In the overridden onStartLoading(), I have:

@Override
protected void onStartLoading() {
  if (mData != null) {
    deliverResult(mData);
  }
  else{
    forceLoad();
  }
}

The containing SherlockListFragment initiates the loader in onActivityCreated:

@Override
public void onActivityCreated(Bundle savedInstanceState) {
  super.onActivityCreated(savedInstanceState);
  mAdapter = new MyListAdapter(getActivity());
  setListAdapter(mAdapter);
  getLoaderManager().initLoader(0, null, this);
}

and :

@Override
public Loader<List<MyData>> onCreateLoader(int id, Bundle args) {
  return new MyListLoader(getActivity());
}

The problem is that after 5 activations/navigations to my FragmentActivity, loadinBackground() is not called. The onStartLoding is called, as well as the forceLoad, but that's it. No Exception, nothing in the LogCat.

Any ideas?

like image 491
Magnus Johansson Avatar asked Feb 10 '13 18:02

Magnus Johansson


1 Answers

It is Ok to call forceLoad().

See what documentation says:
You generally should only call this when the loader is started - that is, isStarted() returns true.

Full code:

@Override
protected void onStartLoading() {
    try {
        if (data != null) {
            deliverResult(data);
        }
        if (takeContentChanged() || data == null) {
            forceLoad();
        }

        Log.d(TAG, "onStartLoading() ");
    } catch (Exception e) {
        Log.d(TAG, e.getMessage());
    }
}

Important:

documentation says: Subclasses of Loader<D> generally must implement at least onStartLoading(), onStopLoading(), onForceLoad(), and onReset().

AsyncTaskLoader extends Loader but not implements onStartLoading(), onStopLoading(), onReset(). You must implement it by yourself!


P.S. I was confused with it after experience of using simple CursorLoader, I also thought that using forceLoad() is bad practice. But it is not true.

like image 112
Yuliia Ashomok Avatar answered Nov 01 '22 21:11

Yuliia Ashomok