Added in API level 11
Deprecated in API level 28

CursorLoader


open class CursorLoader : AsyncTaskLoader<Cursor!>

A loader that queries the ContentResolver and returns a Cursor. This class implements the Loader protocol in a standard way for querying cursors, building on AsyncTaskLoader to perform the cursor query on a background thread so that it does not block the application's UI.

A CursorLoader must be built with the full information for the query to perform, either through the CursorLoader(android.content.Context,android.net.Uri,java.lang.String[],java.lang.String,java.lang.String[],java.lang.String) or creating an empty instance with CursorLoader(android.content.Context) and filling in the desired parameters with setUri(android.net.Uri), setSelection(java.lang.String), setSelectionArgs(java.lang.String[]), setSortOrder(java.lang.String), and setProjection(java.lang.String[]).

Summary

Public constructors
CursorLoader(context: Context!)

Creates an empty unspecified CursorLoader.

CursorLoader(context: Context!, uri: Uri!, projection: Array<String!>!, selection: String!, selectionArgs: Array<String!>!, sortOrder: String!)

Creates a fully-specified CursorLoader.

Public methods
open Unit

open Unit

open Unit
dump(prefix: String!, fd: FileDescriptor!, writer: PrintWriter!, args: Array<String!>!)

open Array<String!>!

open String!

open Array<String!>!

open String!

open Uri!

open Cursor!

open Unit
onCanceled(cursor: Cursor!)

open Unit
setProjection(projection: Array<String!>!)

open Unit
setSelection(selection: String!)

open Unit
setSelectionArgs(selectionArgs: Array<String!>!)

open Unit
setSortOrder(sortOrder: String!)

open Unit
setUri(uri: Uri!)

Protected methods
open Unit

open Unit

Starts an asynchronous load of the data.

open Unit

Must be called from the UI thread

Inherited functions
Unit abandon()

This function will normally be called for you automatically by android.app.LoaderManager when restarting a Loader. When using a Loader with android.app.LoaderManager, you must not call this method yourself, or you will conflict with its management of the Loader. Tell the Loader that it is being abandoned. This is called prior to reset to have it retain its current data but not report any new data.

Boolean cancelLoad()

Attempt to cancel the current load task. Must be called on the main thread of the process.

Cancellation is not an immediate operation, since the load is performed in a background thread. If there is currently a load in progress, this method requests that the load be canceled, and notes this is the case; once the background thread has completed its work its remaining state will be cleared. If another load request comes in during this time, it will be held until the canceled load is complete.

Unit commitContentChanged()

Commit that you have actually fully processed a content change that was returned by takeContentChanged. This is for use with rollbackContentChanged() to handle situations where a load is cancelled. Call this when you have completely processed a load without it being cancelled.

String! dataToString(data: D)

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

Unit deliverCancellation()

Informs the registered OnLoadCanceledListener that the load has been canceled. Should only be called by subclasses. Must be called from the process's main thread.

Unit deliverResult(data: D)

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.

Unit forceLoad()

Force an asynchronous load. Unlike startLoading() this will ignore a previously loaded data set and load a new one. This simply calls through to the implementation's onForceLoad(). You generally should only call this when the loader is started -- that is, isStarted() returns true.

Must be called from the process's main thread.

Context! getContext()

Int getId()

Boolean isAbandoned()

Return whether this loader has been abandoned. In this state, the loader must not report any new data, and must keep its last reported data valid until it is finally reset.

Boolean isReset()

Return whether this load has been reset. That is, either the loader has not yet been started for the first time, or its reset() has been called.

Boolean isStarted()

Return whether this load has been started. That is, its startLoading() has been called and no calls to stopLoading() or reset() have yet been made.

Unit onAbandon()

Subclasses implement this to take care of being abandoned. This is an optional intermediate state prior to onReset() -- it means that the client is no longer interested in any new data from the loader, so the loader must not report any further updates. However, the loader must keep its last reported data valid until the final onReset() happens. You can retrieve the current abandoned state with isAbandoned.

Unit onContentChanged()

Called when ForceLoadContentObserver detects a change. The default implementation checks to see if the loader is currently started; if so, it simply calls forceLoad(); otherwise, it sets a flag so that takeContentChanged() returns true.

Must be called from the process's main thread.

Unit 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.

Unit onStartLoading()

Subclasses must implement this to take care of loading their data, as per startLoading(). This is not called by clients directly, but as a result of a call to startLoading().

Unit onStopLoading()

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

Unit registerListener(id: Int, listener: Loader.OnLoadCompleteListener<D>!)

Registers a class that will receive callbacks when a load is complete. The callback will be called on the process's main thread so it's safe to pass the results to widgets.

Must be called from the process's main thread.

Unit registerOnLoadCanceledListener(listener: Loader.OnLoadCanceledListener<D>!)

Registers a listener that will receive callbacks when a load is canceled. The callback will be called on the process's main thread so it's safe to pass the results to widgets. Must be called from the process's main thread.

Unit reset()

This function will normally be called for you automatically by android.app.LoaderManager when destroying a Loader. When using a Loader with android.app.LoaderManager, you must not call this method yourself, or you will conflict with its management of the Loader. Resets the state of the Loader. The Loader should at this point free all of its resources, since it may never be called again; however, its startLoading() may later be called at which point it must be able to start running again.

This updates the Loader's internal state so that isStarted() and isReset() will return the correct values, and then calls the implementation's onReset().

Must be called from the process's main thread.

Unit rollbackContentChanged()

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. This is to handle the case where a data load due to a content change has been canceled before its data was delivered back to the loader.

Unit startLoading()

This function will normally be called for you automatically by android.app.LoaderManager when the associated fragment/activity is being started. When using a Loader with android.app.LoaderManager, you must not call this method yourself, or you will conflict with its management of the Loader. Starts an asynchronous load of the Loader's data. When the result is ready the callbacks will be called on the process's main thread. If a previous load has been completed and is still valid the result may be passed to the callbacks immediately. The loader will monitor the source of the data set and may deliver future callbacks if the source changes. Calling stopLoading will stop the delivery of callbacks.

This updates the Loader's internal state so that isStarted() and isReset() will return the correct values, and then calls the implementation's onStartLoading().

Must be called from the process's main thread.

Unit stopLoading()

This function will normally be called for you automatically by android.app.LoaderManager when the associated fragment/activity is being stopped. When using a Loader with android.app.LoaderManager, you must not call this method yourself, or you will conflict with its management of the Loader.

Stops delivery of updates until the next time startLoading() is called. Implementations should not invalidate their data at this point -- clients are still free to use the last data the loader reported. They will, however, typically stop reporting new data if the data changes; they can still monitor for changes, but must not report them to the client until and if startLoading() is later called.

This updates the Loader's internal state so that isStarted() will return the correct value, and then calls the implementation's onStopLoading().

Must be called from the process's main thread.

Boolean takeContentChanged()

Take the current flag indicating whether the loader's content had changed while it was stopped. If it had, true is returned and the flag is cleared.

String toString()

Unit unregisterListener(listener: Loader.OnLoadCompleteListener<D>!)

Remove a listener that was previously added with registerListener. Must be called from the process's main thread.

Unit unregisterOnLoadCanceledListener(listener: Loader.OnLoadCanceledListener<D>!)

Unregisters a listener that was previously added with registerOnLoadCanceledListener. Must be called from the process's main thread.

Boolean isLoadInBackgroundCanceled()

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

Boolean onCancelLoad()

Unit onForceLoad()

D onLoadInBackground()

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

Unit setUpdateThrottle(delayMS: Long)

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.

Public constructors

CursorLoader

Added in API level 11
CursorLoader(context: Context!)

Creates an empty unspecified CursorLoader. You must follow this with calls to setUri(android.net.Uri), setSelection(java.lang.String), etc to specify the query to perform.

CursorLoader

Added in API level 11
CursorLoader(
    context: Context!,
    uri: Uri!,
    projection: Array<String!>!,
    selection: String!,
    selectionArgs: Array<String!>!,
    sortOrder: String!)

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

Added in API level 16
Deprecated in API level 28
open fun cancelLoadInBackground(): Unit

Deprecated: Deprecated in Java.

deliverResult

Added in API level 11
open fun deliverResult(cursor: Cursor!): Unit

Deprecated: Deprecated in Java.

Parameters
data the result of the load

dump

Added in API level 11
open fun dump(
    prefix: String!,
    fd: FileDescriptor!,
    writer: PrintWriter!,
    args: Array<String!>!
): Unit

Deprecated: Deprecated in Java.

Parameters
prefix String!: Text to print at the front of each line.
fd FileDescriptor!: The raw file descriptor that the dump is being sent to.
writer PrintWriter!: A PrintWriter to which the dump is to be set.
args Array<String!>!: Additional arguments to the dump request.

getProjection

Added in API level 11
open fun getProjection(): Array<String!>!

Deprecated: Deprecated in Java.

getSelection

Added in API level 11
open fun getSelection(): String!

Deprecated: Deprecated in Java.

getSelectionArgs

Added in API level 11
open fun getSelectionArgs(): Array<String!>!

Deprecated: Deprecated in Java.

getSortOrder

Added in API level 11
open fun getSortOrder(): String!

Deprecated: Deprecated in Java.

getUri

Added in API level 11
open fun getUri(): Uri!

Deprecated: Deprecated in Java.

loadInBackground

Added in API level 11
open fun loadInBackground(): Cursor!

Deprecated: Deprecated in Java.

Return
Cursor! The result of the load operation.
Exceptions
android.os.OperationCanceledException if the load is canceled during execution.

onCanceled

Added in API level 11
open fun onCanceled(cursor: Cursor!): Unit

Deprecated: Deprecated in Java.

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

setProjection

Added in API level 11
open fun setProjection(projection: Array<String!>!): Unit

Deprecated: Deprecated in Java.

setSelection

Added in API level 11
open fun setSelection(selection: String!): Unit

Deprecated: Deprecated in Java.

setSelectionArgs

Added in API level 11
open fun setSelectionArgs(selectionArgs: Array<String!>!): Unit

Deprecated: Deprecated in Java.

setSortOrder

Added in API level 11
open fun setSortOrder(sortOrder: String!): Unit

Deprecated: Deprecated in Java.

setUri

Added in API level 11
open fun setUri(uri: Uri!): Unit

Deprecated: Deprecated in Java.

Protected methods

onReset

Added in API level 11
protected open fun onReset(): Unit

Deprecated: Deprecated in Java.

onStartLoading

Added in API level 11
protected open fun onStartLoading(): Unit

Deprecated: Deprecated in Java.

Starts an asynchronous load of the 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

Added in API level 11
protected open fun onStopLoading(): Unit

Deprecated: Deprecated in Java.

Must be called from the UI thread