CursorLoader

public class CursorLoader extends AsyncTaskLoader


Static library support version of the framework's android.content.CursorLoader. 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

Creates an empty unspecified CursorLoader.

CursorLoader(
    @NonNull Context context,
    @NonNull Uri uri,
    @Nullable String[] projection,
    @Nullable String selection,
    @Nullable String[] selectionArgs,
    @Nullable String sortOrder
)

Creates a fully-specified CursorLoader.

Public methods

void

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

void

Sends the result of the load to the registered listener.

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.

@Nullable String[]
@Nullable String
@Nullable String[]
@Nullable String
@NonNull Uri
Cursor

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
setProjection(@Nullable String[] projection)
void
void
setSelectionArgs(@Nullable String[] selectionArgs)
void
void

Protected methods

void

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

void

Starts an asynchronous load of the contacts list data.

void

Must be called from the UI thread

Inherited methods

From androidx.loader.content.AsyncTaskLoader
@NonNull Executor

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

boolean

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

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.

void
setUpdateThrottle(long delayMS)

Set amount to throttle updates by.

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

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

CursorLoader

Added in 1.0.0
public CursorLoader(@NonNull Context context)

Creates an empty unspecified CursorLoader. You must follow this with calls to setUri, setSelection, etc to specify the query to perform.

CursorLoader

Added in 1.0.0
public CursorLoader(
    @NonNull Context context,
    @NonNull Uri uri,
    @Nullable String[] projection,
    @Nullable String selection,
    @Nullable String[] selectionArgs,
    @Nullable String sortOrder
)

Creates a fully-specified CursorLoader. See ContentResolver.query() for documentation on the meaning of the parameters. These will be passed as-is to that call.

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

deliverResult

Added in 1.0.0
public void deliverResult(Cursor data)

Sends the result of the load to the registered listener. Should only be called by subclasses. Must be called from the process's main thread.

Parameters
Cursor data

the result of the load

dump

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

getProjection

Added in 1.0.0
public @Nullable String[] getProjection()

getSelection

Added in 1.0.0
public @Nullable String getSelection()

getSelectionArgs

Added in 1.0.0
public @Nullable String[] getSelectionArgs()

getSortOrder

Added in 1.0.0
public @Nullable String getSortOrder()

getUri

Added in 1.0.0
public @NonNull Uri getUri()

loadInBackground

Added in 1.2.0-alpha01
public Cursor loadInBackground()

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
Cursor

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(Cursor 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
Cursor data

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

setProjection

Added in 1.0.0
public void setProjection(@Nullable String[] projection)

setSelection

Added in 1.0.0
public void setSelection(@Nullable String selection)

setSelectionArgs

Added in 1.0.0
public void setSelectionArgs(@Nullable String[] selectionArgs)

setSortOrder

Added in 1.0.0
public void setSortOrder(@Nullable String sortOrder)

setUri

Added in 1.0.0
public void setUri(@NonNull Uri uri)

Protected methods

onReset

protected void onReset()

Subclasses must implement this to take care of resetting their loader, as per reset. This is not called by clients directly, but as a result of a call to reset. This will always be called from the process's main thread.

onStartLoading

protected void onStartLoading()

Starts an asynchronous load of the contacts list data. When the result is ready the callbacks will be called on the UI thread. If a previous load has been completed and is still valid the result may be passed to the callbacks immediately. Must be called from the UI thread

onStopLoading

protected void onStopLoading()

Must be called from the UI thread