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;
}
}
}