Skip to content

Instantly share code, notes, and snippets.

Last active Aug 29, 2015
What would you like to do?
public abstract class AbstractNetService<K> extends AsyncTaskLoader<K> {
private Bundle bundle;
private K mData;
public AbstractNetService(Context context, Bundle bundle) {
this.bundle = bundle;
protected void onStartLoading() {
if (mData != null) {
// Deliver any previously loaded data immediately.
if (takeContentChanged() || mData == null) {
// When the observer detects a change, it should call onContentChanged()
// on the Loader, which will cause the next call to takeContentChanged()
// to return true. If this is ever the case (or if the current data is
// null), we force a new load.
protected void onStopLoading() {
// The Loader is in a stopped state, so we should attempt to cancel the
// current load (if there is one).
// Note that we leave the observer as is. Loaders in a stopped state
// should still monitor the data source for changes so that the Loader
// will know to force a new load if it is ever started again.
protected void onReset() {
// Ensure the loader has been stopped.
// At this point we can release the resources associated with 'mData'.
if (mData != null) {
mData = null;
public void onCanceled(K data) {
// Attempt to cancel the current asynchronous load.
// The load has been canceled, so we should release the resources
// associated with 'data'.
public void deliverResult(K data) {
if (isReset()) {
// The Loader has been reset; ignore the result and invalidate the data.
// Hold a reference to the old data so it doesn't get garbage collected.
// We must protect it until the new data has been delivered.
K oldData = mData;
mData = data;
if (isStarted()) {
// If the Loader is in a started state, deliver the results to the
// client. The superclass method does this for us.
// Invalidate the old data as we don't need it any more.
if (oldData != null && oldData != data) {
public K loadInBackground() {
try {
RestAdapter restAdapter = new RestAdapter.Builder()
return loadInBackground(restAdapter, bundle);
} catch (Exception e) {
return null;
protected abstract K loadInBackground(RestAdapter restAdapter, Bundle args) throws Exception;
* /For a simple List, there is nothing to do. For something like a Cursor, we
* would close it in this method. All resources associated with the Loader
* should be released here.
* @param data
protected abstract void releaseResources(K data);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment