D
- the data type to be loaded.public abstract class AsyncTaskLoader<D> extends Loader<D>
AsyncTask
to do the work. See
Loader
and LoaderManager
for more details.
Here is an example implementation of an AsyncTaskLoader subclass that loads the currently installed applications from the package manager. This implementation takes care of retrieving the application labels and sorting its result set from them, monitoring for changes to the installed applications, and rebuilding the list when a change in configuration requires this (such as a locale change).
An example implementation of a fragment that uses the above loader to show the currently installed applications in a list is below.
Loader.ForceLoadContentObserver, Loader.OnLoadCanceledListener<D>, Loader.OnLoadCompleteListener<D>
Constructor and Description |
---|
AsyncTaskLoader(Context context) |
AsyncTaskLoader(Context context,
Executor executor) |
Modifier and Type | Method and Description |
---|---|
void |
cancelLoadInBackground()
Called on the main thread to abort a load in progress.
|
void |
dump(String prefix,
FileDescriptor fd,
PrintWriter writer,
String[] args)
Print the Loader's state into the given stream.
|
boolean |
isLoadInBackgroundCanceled()
Returns true if the current invocation of
loadInBackground() is being canceled. |
abstract D |
loadInBackground()
Called on a worker thread to perform the actual load and to return
the result of the load operation.
|
void |
onCanceled(D data)
Called if the task was canceled before it was completed.
|
protected boolean |
onCancelLoad()
Subclasses must implement this to take care of requests to
Loader.cancelLoad() . |
protected void |
onForceLoad()
Subclasses must implement this to take care of requests to
Loader.forceLoad() . |
protected D |
onLoadInBackground()
Calls
loadInBackground() . |
void |
setUpdateThrottle(long delayMS)
Set amount to throttle updates by.
|
void |
waitForLoader()
Locks the current thread until the loader completes the current load
operation.
|
abandon, cancelLoad, commitContentChanged, dataToString, deliverCancellation, deliverResult, forceLoad, getContext, getId, isAbandoned, isReset, isStarted, onAbandon, onContentChanged, onReset, onStartLoading, onStopLoading, registerListener, registerOnLoadCanceledListener, reset, rollbackContentChanged, startLoading, stopLoading, takeContentChanged, toString, unregisterListener, unregisterOnLoadCanceledListener
public AsyncTaskLoader(Context context)
public void setUpdateThrottle(long delayMS)
loadInBackground()
call has completed until
a new load is scheduled.delayMS
- Amount of delay, in milliseconds.protected void onForceLoad()
Loader
Loader.forceLoad()
.
This will always be called from the process's main thread.onForceLoad
in class Loader<D>
protected boolean onCancelLoad()
Loader
Loader.cancelLoad()
.
This will always be called from the process's main thread.onCancelLoad
in class Loader<D>
Loader.startLoading()
hasn't been called; returns
true otherwise. When true is returned, the task
is still running and the Loader.OnLoadCanceledListener
will be called
when the task completes.public void onCanceled(D data)
data
- The value that was returned by loadInBackground()
, or null
if the task threw OperationCanceledException
.public abstract D loadInBackground()
Loader.deliverResult(D)
on
the UI thread. If implementations need to process the results on the UI thread
they may override Loader.deliverResult(D)
and do so there.
To support cancellation, this method should periodically check the value of
isLoadInBackgroundCanceled()
and terminate when it returns true.
Subclasses may also override cancelLoadInBackground()
to interrupt the load
directly instead of polling isLoadInBackgroundCanceled()
.
When the load is canceled, this method may either return normally or throw
OperationCanceledException
. In either case, the Loader
will
call onCanceled(D)
to perform post-cancellation cleanup and to dispose of the
result object, if any.OperationCanceledException
- if the load is canceled during execution.isLoadInBackgroundCanceled()
,
cancelLoadInBackground()
,
onCanceled(D)
protected D onLoadInBackground()
loadInBackground()
.
This method is reserved for use by the loader framework.
Subclasses should override loadInBackground()
instead of this method.OperationCanceledException
- if the load is canceled during execution.loadInBackground()
public void cancelLoadInBackground()
loadInBackground()
that is running in the background on a worker thread.
This method should do nothing if loadInBackground()
has not started
running or if it has already finished.loadInBackground()
public boolean isLoadInBackgroundCanceled()
loadInBackground()
is being canceled.loadInBackground()
is being canceled.loadInBackground()
public void waitForLoader()
Use for testing only. Never call this from a UI thread.
public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args)
Loader