Skip to content

Instantly share code, notes, and snippets.

@skyisle
Last active December 14, 2015 05:29
Show Gist options
  • Save skyisle/5035807 to your computer and use it in GitHub Desktop.
Save skyisle/5035807 to your computer and use it in GitHub Desktop.
AsyncLoader
import android.content.Context;
import android.support.v4.content.AsyncTaskLoader;
public abstract class AsyncLoader<D> extends AsyncTaskLoader<D> {
private D data;
public AsyncLoader(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