Last active
June 9, 2016 15:31
-
-
Save starkej2/81afbdde0788eaddc1db1f7f8ab376b4 to your computer and use it in GitHub Desktop.
Example AsyncTaskLoader implementation
This file contains 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
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