Created
February 3, 2014 14:52
-
-
Save wasabeef/8785201 to your computer and use it in GitHub Desktop.
Android - AbstractAsyncLoader
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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