Notifications Save this page to your Developer Profile to get notifications on important updates. Stay organized with collections Save and categorize content based on your preferences.

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 class androidx.loader.content.Loader
@MainThread void

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

@MainThread 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.

@MainThread void

Informs the registered OnLoadCanceledListener that the load has been canceled.

@MainThread void

Sends the result of the load to the registered listener.

@MainThread 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.

@MainThread void

Subclasses implement this to take care of being abandoned.

@MainThread void

Called when ForceLoadContentObserver detects a change.

@MainThread void

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

@MainThread void

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

@MainThread void

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

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

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

@MainThread void

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

@MainThread 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 @MainThread void

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

@MainThread 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
@MainThread void

Remove a listener that was previously added with registerListener.

@MainThread void

Unregisters a listener that was previously added with registerOnLoadCanceledListener.

Public constructors

AsyncTaskLoader

public AsyncTaskLoader(@NonNull Context context)

Public methods

cancelLoadInBackground

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

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

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 androidx.core.os.OperationCanceledException

if the load is canceled during execution.

onCanceled

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

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

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

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 androidx.core.os.OperationCanceledException

if the load is canceled during execution.

See also
loadInBackground