AsyncTaskLoader

public abstract class AsyncTaskLoader<D> extends Loader

Known direct subclasses
CursorLoader

Static library support version of the framework's android.content.CursorLoader.


Static library support version of the framework's android.content.AsyncTaskLoader. Used to write apps that run on platforms prior to Android 3.0. When running on Android 3.0 or above, this implementation is still used; it does not try to switch to the framework's implementation. See the framework SDK documentation for a class overview.

Summary

Public constructors

Public methods

void

Called on the main thread to abort a load in progress.

void
dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args)

This method is deprecated.

Consider using enableDebugLogging to understand the series of operations performed by LoaderManager.

boolean

Returns true if the current invocation of loadInBackground is being canceled.

abstract @Nullable D

Called on a worker thread to perform the actual load and to return the result of the load operation.

void

Called if the task was canceled before it was completed.

void
setUpdateThrottle(long delayMS)

Set amount to throttle updates by.

Protected methods

@NonNull Executor

Returns the Executor to use for this Loader's AsyncTasks.

boolean

Subclasses must implement this to take care of requests to cancelLoad.

void

Subclasses must implement this to take care of requests to forceLoad.

@Nullable D

Calls loadInBackground.

Inherited methods

From androidx.loader.content.Loader
void

This function will normally be called for you automatically by LoaderManager when restarting a Loader.

boolean

Attempt to cancel the current load task.

void

Commit that you have actually fully processed a content change that was returned by takeContentChanged.

@NonNull String

For debugging, converts an instance of the Loader's data class to a string that can be printed.

void

Informs the registered OnLoadCanceledListener that the load has been canceled.

void

Sends the result of the load to the registered listener.

void

Force an asynchronous load.

@NonNull Context
int
boolean

Return whether this loader has been abandoned.

boolean

Return whether this load has been reset.

boolean

Return whether this load has been started.

void

Subclasses implement this to take care of being abandoned.

void

Called when ForceLoadContentObserver detects a change.

void

Subclasses must implement this to take care of resetting their loader, as per reset.

void

Subclasses must implement this to take care of loading their data, as per startLoading.

void

Subclasses must implement this to take care of stopping their loader, as per stopLoading.

void
@MainThread
registerListener(
    int id,
    @NonNull Loader.OnLoadCompleteListener<D> listener
)

Registers a class that will receive callbacks when a load is complete.

void

Registers a listener that will receive callbacks when a load is canceled.

void

This function will normally be called for you automatically by LoaderManager when destroying a Loader.

void

Report that you have abandoned the processing of a content change that was returned by takeContentChanged and would like to rollback to the state where there is again a pending content change.

final void

This function will normally be called for you automatically by LoaderManager when the associated fragment/activity is being started.

void

This function will normally be called for you automatically by LoaderManager when the associated fragment/activity is being stopped.

boolean

Take the current flag indicating whether the loader's content had changed while it was stopped.

@NonNull String
void

Remove a listener that was previously added with registerListener.

void

Unregisters a listener that was previously added with registerOnLoadCanceledListener.

Public constructors

AsyncTaskLoader

Added in 1.0.0
public AsyncTaskLoader(@NonNull Context context)

Public methods

cancelLoadInBackground

Added in 1.0.0
public void cancelLoadInBackground()

Called on the main thread to abort a load in progress. Override this method to abort the current invocation of 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.

See also
loadInBackground

dump

public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args)

isLoadInBackgroundCanceled

Added in 1.0.0
public boolean isLoadInBackgroundCanceled()

Returns true if the current invocation of loadInBackground is being canceled.

Returns
boolean

True if the current invocation of loadInBackground is being canceled.

See also
loadInBackground

loadInBackground

Added in 1.0.0
public abstract @NullableloadInBackground()

Called on a worker thread to perform the actual load and to return the result of the load operation. Implementations should not deliver the result directly, but should return them from this method, which will eventually end up calling deliverResult on the UI thread. If implementations need to process the results on the UI thread they may override deliverResult 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 to perform post-cancellation cleanup and to dispose of the result object, if any.

Returns
@Nullable D

The result of the load operation.

Throws
androidx.core.os.OperationCanceledException

if the load is canceled during execution.

onCanceled

Added in 1.0.0
public void onCanceled(@Nullable D data)

Called if the task was canceled before it was completed. Gives the class a chance to clean up post-cancellation and to properly dispose of the result.

Parameters
@Nullable D data

The value that was returned by loadInBackground, or null if the task threw OperationCanceledException.

setUpdateThrottle

Added in 1.0.0
public void setUpdateThrottle(long delayMS)

Set amount to throttle updates by. This is the minimum time from when the last loadInBackground call has completed until a new load is scheduled.

Parameters
long delayMS

Amount of delay, in milliseconds.

Protected methods

getExecutor

Added in 1.1.0
protected @NonNull Executor getExecutor()

Returns the Executor to use for this Loader's AsyncTasks. By default THREAD_POOL_EXECUTOR will be used. Override this method to return a custom executor. Note that this method will only be called once before this Loader's first AsyncTask is run. It is up to the Loader to shut down the Executor at the appropriate place (e.g. in onAbandon) if necessary.

Returns
@NonNull Executor

the Executor to use for this Loader's AsyncTasks.

onCancelLoad

protected boolean onCancelLoad()

Subclasses must implement this to take care of requests to cancelLoad. This will always be called from the process's main thread.

Returns
boolean

Returns false if the task could not be canceled, typically because it has already completed normally, or because startLoading hasn't been called; returns true otherwise. When true is returned, the task is still running and the OnLoadCanceledListener will be called when the task completes.

onForceLoad

protected void onForceLoad()

Subclasses must implement this to take care of requests to forceLoad. This will always be called from the process's main thread.

onLoadInBackground

Added in 1.0.0
protected @NullableonLoadInBackground()

Calls loadInBackground. This method is reserved for use by the loader framework. Subclasses should override loadInBackground instead of this method.

Returns
@Nullable D

The result of the load operation.

Throws
androidx.core.os.OperationCanceledException

if the load is canceled during execution.

See also
loadInBackground