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?
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());
}
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With