Created
May 12, 2013 10:01
-
-
Save keyboardr/5563035 to your computer and use it in GitHub Desktop.
A handy AsyncTaskLoader that contains common caching functionality.
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
import android.content.Context; | |
import android.support.v4.content.AsyncTaskLoader; | |
public abstract class CachedLoader<D> extends AsyncTaskLoader<D> { | |
private D mResult; | |
public CachedLoader(Context context) { | |
super(context); | |
} | |
@Override | |
protected void onStartLoading() { | |
super.onStartLoading(); | |
if (mResult != null) { | |
deliverResult(mResult); | |
} | |
if (takeContentChanged() || mResult == null) { | |
forceLoad(); | |
} | |
} | |
@Override | |
public void deliverResult(D data) { | |
mResult = data; | |
if (isStarted()) { | |
super.deliverResult(data); | |
} | |
} | |
@Override | |
protected void onStopLoading() { | |
super.onStopLoading(); | |
cancelLoad(); | |
} | |
@Override | |
protected void onReset() { | |
super.onReset(); | |
onStopLoading(); | |
mResult = null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment