Skip to content

Instantly share code, notes, and snippets.

@starkej2
Last active June 9, 2016 15:31
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 starkej2/81afbdde0788eaddc1db1f7f8ab376b4 to your computer and use it in GitHub Desktop.
Save starkej2/81afbdde0788eaddc1db1f7f8ab376b4 to your computer and use it in GitHub Desktop.
Example AsyncTaskLoader implementation
public class MyAsyncTaskLoader extends AsyncTaskLoader<ResponseObject> {
private String mId;
private ResponseObject mResponse;
/**
* Creates a new {@link MyAsyncTaskLoader}.
*
* @param context The context to use.
* @param id The ID of the item to load.
*/
public MyAsyncTaskLoader(@NonNull Context context, @NonNull String id) {
super(context);
mId = id;
}
@Override
protected void onStartLoading() {
if (mResponse != null) {
deliverResult(mResponse);
}
if (mResponse == null || takeContentChanged()) {
forceLoad();
}
}
@Override
public ResponseObject loadInBackground() {
return new ResponseObject(mId); // load then return data
}
@Override
public void deliverResult(ResponseObject response) {
if (isReset()) {
return;
}
mResponse = response;
if (isStarted()) {
super.deliverResult(mResponse);
}
}
@Override
protected void onReset() {
onStopLoading();
if (mResponse != null) {
mResponse = null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment