Skip to content

Instantly share code, notes, and snippets.

@wasabeef
Created February 3, 2014 14:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wasabeef/8785201 to your computer and use it in GitHub Desktop.
Save wasabeef/8785201 to your computer and use it in GitHub Desktop.
Android - AbstractAsyncLoader
import android.content.Context;
import android.support.v4.content.AsyncTaskLoader;
public abstract class AbstractAsyncLoader<D> extends AsyncTaskLoader<D> {
private D data;
public AbstractAsyncLoader(Context context) {
super(context);
}
@Override
public void deliverResult(D data) {
if (isReset()) {
// An async query came in while the loader is stopped
return;
}
this.data = data;
super.deliverResult(data);
}
@Override
protected void onStartLoading() {
if (data != null) {
deliverResult(data);
}
if (takeContentChanged() || data == null) {
forceLoad();
}
}
@Override
protected void onStopLoading() {
// Attempt to cancel the current load task if possible.
cancelLoad();
}
@Override
protected void onReset() {
super.onReset();
// Ensure the loader is stopped
onStopLoading();
data = null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment