# CursorLoader

Artifact: [androidx.loader:loader](https://developer.android.com/jetpack/androidx/releases/loader) [View Source](https://cs.android.com/search?q=file:androidx/loader/content/CursorLoader.java+class:androidx.loader.content.CursorLoader) Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/loader#1.0.0)

*** ** * ** ***


```
public class CursorLoader extends AsyncTaskLoader<Cursor>
```

<br />

|---|---|---|---|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object.html) ||||
| ↳ | [androidx.loader.content.Loader](https://developer.android.com/reference/androidx/loader/content/Loader) |||
|   | ↳ | [androidx.loader.content.AsyncTaskLoader](https://developer.android.com/reference/androidx/loader/content/AsyncTaskLoader)\<[Cursor](https://developer.android.com/reference/android/database/Cursor.html)\> ||
|   |   | ↳ | [androidx.loader.content.CursorLoader](https://developer.android.com/reference/androidx/loader/content/CursorLoader) |

*** ** * ** ***

Static library support version of the framework's `https://developer.android.com/reference/android/content/CursorLoader.html`. 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 |
|---|
| `https://developer.android.com/reference/androidx/loader/content/CursorLoader#CursorLoader(android.content.Context)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/Context.html context)` Creates an empty unspecified CursorLoader. |
| `https://developer.android.com/reference/androidx/loader/content/CursorLoader#CursorLoader(android.content.Context,android.net.Uri,java.lang.String[],java.lang.String,java.lang.String[],java.lang.String)( @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/Context.html context, @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/net/Uri.html uri, @https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html String[] projection, @https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/java/lang/String.html selection, @https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html String[] selectionArgs, @https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/java/lang/String.html sortOrder )` Creates a fully-specified CursorLoader. |

| ### Public methods |
|---|---|
| `void` | `https://developer.android.com/reference/androidx/loader/content/CursorLoader#cancelLoadInBackground()()` Called on the main thread to abort a load in progress. |
| `void` | `https://developer.android.com/reference/androidx/loader/content/CursorLoader#deliverResult(android.database.Cursor)(@https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/android/database/Cursor.html data)` Sends the result of the load to the registered listener. |
| `void` | `[dump](https://developer.android.com/reference/androidx/loader/content/CursorLoader#dump(java.lang.String,java.io.FileDescriptor,java.io.PrintWriter,java.lang.String[]))( @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/lang/String.html prefix, @https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/java/io/FileDescriptor.html fd, @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/io/PrintWriter.html writer, @https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html String[] args )` **This method is deprecated.** Consider using `https://developer.android.com/reference/androidx/loader/app/LoaderManager#enableDebugLogging(boolean)` to understand the series of operations performed by LoaderManager. <br /> |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html String[]` | `https://developer.android.com/reference/androidx/loader/content/CursorLoader#getProjection()()` |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/java/lang/String.html` | `https://developer.android.com/reference/androidx/loader/content/CursorLoader#getSelection()()` |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html String[]` | `https://developer.android.com/reference/androidx/loader/content/CursorLoader#getSelectionArgs()()` |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/java/lang/String.html` | `https://developer.android.com/reference/androidx/loader/content/CursorLoader#getSortOrder()()` |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/net/Uri.html` | `https://developer.android.com/reference/androidx/loader/content/CursorLoader#getUri()()` |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/android/database/Cursor.html` | `https://developer.android.com/reference/androidx/loader/content/CursorLoader#loadInBackground()()` Called on a worker thread to perform the actual load and to return the result of the load operation. |
| `void` | `https://developer.android.com/reference/androidx/loader/content/CursorLoader#onCanceled(android.database.Cursor)(@https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/android/database/Cursor.html data)` Called if the task was canceled before it was completed. |
| `void` | `https://developer.android.com/reference/androidx/loader/content/CursorLoader#setProjection(java.lang.String[])(@https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html String[] projection)` |
| `void` | `https://developer.android.com/reference/androidx/loader/content/CursorLoader#setSelection(java.lang.String)(@https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/java/lang/String.html selection)` |
| `void` | `https://developer.android.com/reference/androidx/loader/content/CursorLoader#setSelectionArgs(java.lang.String[])(@https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html String[] selectionArgs)` |
| `void` | `https://developer.android.com/reference/androidx/loader/content/CursorLoader#setSortOrder(java.lang.String)(@https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/java/lang/String.html sortOrder)` |
| `void` | `https://developer.android.com/reference/androidx/loader/content/CursorLoader#setUri(android.net.Uri)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/net/Uri.html uri)` |

| ### Protected methods |
|---|---|
| `void` | `https://developer.android.com/reference/androidx/loader/content/CursorLoader#onReset()()` Subclasses must implement this to take care of resetting their loader, as per `https://developer.android.com/reference/androidx/loader/content/Loader#reset()`. |
| `void` | `https://developer.android.com/reference/androidx/loader/content/CursorLoader#onStartLoading()()` Starts an asynchronous load of the contacts list data. |
| `void` | `https://developer.android.com/reference/androidx/loader/content/CursorLoader#onStopLoading()()` Must be called from the UI thread |

### Inherited methods

From [androidx.loader.content.AsyncTaskLoader](https://developer.android.com/reference/androidx/loader/content/AsyncTaskLoader) `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/util/concurrent/Executor.html` `https://developer.android.com/reference/androidx/loader/content/AsyncTaskLoader#getExecutor()()`

Returns the `https://developer.android.com/reference/java/util/concurrent/Executor.html` to use for this `https://developer.android.com/reference/androidx/loader/content/Loader`'s `https://developer.android.com/reference/android/os/AsyncTask.html`s.
`boolean` `https://developer.android.com/reference/androidx/loader/content/AsyncTaskLoader#isLoadInBackgroundCanceled()()`

Returns true if the current invocation of `https://developer.android.com/reference/androidx/loader/content/AsyncTaskLoader#loadInBackground()` is being canceled.
`boolean` `https://developer.android.com/reference/androidx/loader/content/AsyncTaskLoader#onCancelLoad()()`

Subclasses must implement this to take care of requests to `https://developer.android.com/reference/androidx/loader/content/Loader#cancelLoad()`.
`void` `https://developer.android.com/reference/androidx/loader/content/AsyncTaskLoader#onForceLoad()()`

Subclasses must implement this to take care of requests to `https://developer.android.com/reference/androidx/loader/content/Loader#forceLoad()`.
`@https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html D` `https://developer.android.com/reference/androidx/loader/content/AsyncTaskLoader#onLoadInBackground()()`

Calls `https://developer.android.com/reference/androidx/loader/content/AsyncTaskLoader#loadInBackground()`.
`void` `https://developer.android.com/reference/androidx/loader/content/AsyncTaskLoader#setUpdateThrottle(long)(long delayMS)`

Set amount to throttle updates by.
From [androidx.loader.content.Loader](https://developer.android.com/reference/androidx/loader/content/Loader) `void` `@https://developer.android.com/reference/androidx/annotation/MainThread
https://developer.android.com/reference/androidx/loader/content/Loader#abandon()()`

This function will normally be called for you automatically by `https://developer.android.com/reference/androidx/loader/app/LoaderManager` when restarting a Loader.
`boolean` `@https://developer.android.com/reference/androidx/annotation/MainThread
https://developer.android.com/reference/androidx/loader/content/Loader#cancelLoad()()`

Attempt to cancel the current load task.
`void` `https://developer.android.com/reference/androidx/loader/content/Loader#commitContentChanged()()`

Commit that you have actually fully processed a content change that was returned by `https://developer.android.com/reference/androidx/loader/content/Loader#takeContentChanged()`.
`@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/lang/String.html` `https://developer.android.com/reference/androidx/loader/content/Loader#dataToString(D)(@https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html D data)`

For debugging, converts an instance of the Loader's data class to a string that can be printed.
`void` `@https://developer.android.com/reference/androidx/annotation/MainThread
https://developer.android.com/reference/androidx/loader/content/Loader#deliverCancellation()()`

Informs the registered `https://developer.android.com/reference/androidx/loader/content/Loader.OnLoadCanceledListener` that the load has been canceled.
`void` `@https://developer.android.com/reference/androidx/annotation/MainThread
https://developer.android.com/reference/androidx/loader/content/Loader#forceLoad()()`

Force an asynchronous load.
`@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/Context.html` `https://developer.android.com/reference/androidx/loader/content/Loader#getContext()()` `int` `https://developer.android.com/reference/androidx/loader/content/Loader#getId()()` `boolean` `https://developer.android.com/reference/androidx/loader/content/Loader#isAbandoned()()`

Return whether this loader has been abandoned.
`boolean` `https://developer.android.com/reference/androidx/loader/content/Loader#isReset()()`

Return whether this load has been reset.
`boolean` `https://developer.android.com/reference/androidx/loader/content/Loader#isStarted()()`

Return whether this load has been started.
`void` `@https://developer.android.com/reference/androidx/annotation/MainThread
https://developer.android.com/reference/androidx/loader/content/Loader#onAbandon()()`

Subclasses implement this to take care of being abandoned.
`void` `@https://developer.android.com/reference/androidx/annotation/MainThread
https://developer.android.com/reference/androidx/loader/content/Loader#onContentChanged()()`

Called when `https://developer.android.com/reference/androidx/loader/content/Loader.ForceLoadContentObserver` detects a change.
`void` `@https://developer.android.com/reference/androidx/annotation/MainThread
https://developer.android.com/reference/androidx/loader/content/Loader#registerListener(int,androidx.loader.content.Loader.OnLoadCompleteListener<D>)(
int id,
@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/loader/content/Loader.OnLoadCompleteListener<D> listener
)`

Registers a class that will receive callbacks when a load is complete.
`void` `@https://developer.android.com/reference/androidx/annotation/MainThread
https://developer.android.com/reference/androidx/loader/content/Loader#registerOnLoadCanceledListener(androidx.loader.content.Loader.OnLoadCanceledListener<D>)(
@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/loader/content/Loader.OnLoadCanceledListener<D> listener
)`

Registers a listener that will receive callbacks when a load is canceled.
`void` `@https://developer.android.com/reference/androidx/annotation/MainThread
https://developer.android.com/reference/androidx/loader/content/Loader#reset()()`

This function will normally be called for you automatically by `https://developer.android.com/reference/androidx/loader/app/LoaderManager` when destroying a Loader.
`void` `https://developer.android.com/reference/androidx/loader/content/Loader#rollbackContentChanged()()`

Report that you have abandoned the processing of a content change that was returned by `https://developer.android.com/reference/androidx/loader/content/Loader#takeContentChanged()` and would like to rollback to the state where there is again a pending content change.
`final void` `@https://developer.android.com/reference/androidx/annotation/MainThread
https://developer.android.com/reference/androidx/loader/content/Loader#startLoading()()`

This function will normally be called for you automatically by `https://developer.android.com/reference/androidx/loader/app/LoaderManager` when the associated fragment/activity is being started.
`void` `@https://developer.android.com/reference/androidx/annotation/MainThread
https://developer.android.com/reference/androidx/loader/content/Loader#stopLoading()()`

This function will normally be called for you automatically by `https://developer.android.com/reference/androidx/loader/app/LoaderManager` when the associated fragment/activity is being stopped.
`boolean` `https://developer.android.com/reference/androidx/loader/content/Loader#takeContentChanged()()`

Take the current flag indicating whether the loader's content had changed while it was stopped.
`@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/lang/String.html` `https://developer.android.com/reference/androidx/loader/content/Loader#toString()()` `void` `@https://developer.android.com/reference/androidx/annotation/MainThread
https://developer.android.com/reference/androidx/loader/content/Loader#unregisterListener(androidx.loader.content.Loader.OnLoadCompleteListener<D>)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/loader/content/Loader.OnLoadCompleteListener<D> listener)`

Remove a listener that was previously added with `https://developer.android.com/reference/androidx/loader/content/Loader#registerListener(int,androidx.loader.content.Loader.OnLoadCompleteListener<D>)`.
`void` `@https://developer.android.com/reference/androidx/annotation/MainThread
https://developer.android.com/reference/androidx/loader/content/Loader#unregisterOnLoadCanceledListener(androidx.loader.content.Loader.OnLoadCanceledListener<D>)(
@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/loader/content/Loader.OnLoadCanceledListener<D> listener
)`

Unregisters a listener that was previously added with `https://developer.android.com/reference/androidx/loader/content/Loader#registerOnLoadCanceledListener(androidx.loader.content.Loader.OnLoadCanceledListener<D>)`.

## Public constructors

### CursorLoader

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/loader#1.0.0)

```
public CursorLoader(@NonNull Context context)
```

Creates an empty unspecified CursorLoader. You must follow this with calls to `https://developer.android.com/reference/androidx/loader/content/CursorLoader#setUri(android.net.Uri)`, `https://developer.android.com/reference/androidx/loader/content/CursorLoader#setSelection(java.lang.String)`, etc to specify the query to perform.

### CursorLoader

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/loader#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 `https://developer.android.com/reference/android/content/ContentResolver.html#query(android.net.Uri, java.lang.String[], java.lang.String, java.lang.String[], java.lang.String)` 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 `https://developer.android.com/reference/androidx/loader/content/AsyncTaskLoader#loadInBackground()` that is running in the background on a worker thread. This method should do nothing if `https://developer.android.com/reference/androidx/loader/content/AsyncTaskLoader#loadInBackground()` has not started running or if it has already finished.

| See also |
|---|---|
| `https://developer.android.com/reference/androidx/loader/content/AsyncTaskLoader#loadInBackground()` |   |

### deliverResult

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/loader#1.0.0)

```
public void deliverResult(@Nullable 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 |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/android/database/Cursor.html data` | the result of the load |

### dump

```
public void [dump](https://developer.android.com/reference/androidx/loader/content/CursorLoader#dump(java.lang.String,java.io.FileDescriptor,java.io.PrintWriter,java.lang.String[]))(
    @NonNull String prefix,
    @Nullable FileDescriptor fd,
    @NonNull PrintWriter writer,
    @Nullable String[] args
)
```

> [!CAUTION]
> **This method is deprecated.**   
>
> Consider using `https://developer.android.com/reference/androidx/loader/app/LoaderManager#enableDebugLogging(boolean)` to understand the series of operations performed by LoaderManager.

### getProjection

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/loader#1.0.0)

```
public @Nullable String[] getProjection()
```

### getSelection

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/loader#1.0.0)

```
public @Nullable String getSelection()
```

### getSelectionArgs

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/loader#1.0.0)

```
public @Nullable String[] getSelectionArgs()
```

### getSortOrder

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/loader#1.0.0)

```
public @Nullable String getSortOrder()
```

### getUri

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/loader#1.0.0)

```
public @NonNull Uri getUri()
```

### loadInBackground

```
public @Nullable 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 `https://developer.android.com/reference/androidx/loader/content/Loader#deliverResult(D)` on the UI thread. If implementations need to process the results on the UI thread they may override `https://developer.android.com/reference/androidx/loader/content/Loader#deliverResult(D)` and do so there. To support cancellation, this method should periodically check the value of `https://developer.android.com/reference/androidx/loader/content/AsyncTaskLoader#isLoadInBackgroundCanceled()` and terminate when it returns true. Subclasses may also override `https://developer.android.com/reference/androidx/loader/content/AsyncTaskLoader#cancelLoadInBackground()` to interrupt the load directly instead of polling `https://developer.android.com/reference/androidx/loader/content/AsyncTaskLoader#isLoadInBackgroundCanceled()`. When the load is canceled, this method may either return normally or throw `https://developer.android.com/reference/androidx/core/os/OperationCanceledException`. In either case, the `https://developer.android.com/reference/androidx/loader/content/Loader` will call `https://developer.android.com/reference/androidx/loader/content/AsyncTaskLoader#onCanceled(D)` to perform post-cancellation cleanup and to dispose of the result object, if any.

| Returns |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/android/database/Cursor.html` | The result of the load operation. |

| Throws |
|---|---|
| `https://developer.android.com/reference/androidx/core/os/OperationCanceledException` | if the load is canceled during execution. |

| See also |
|---|---|
| `https://developer.android.com/reference/androidx/loader/content/AsyncTaskLoader#isLoadInBackgroundCanceled()` |   |
| `https://developer.android.com/reference/androidx/loader/content/AsyncTaskLoader#cancelLoadInBackground()` |   |
| `https://developer.android.com/reference/androidx/loader/content/AsyncTaskLoader#onCanceled(D)` |   |

### onCanceled

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/loader#1.0.0)

```
public void onCanceled(@Nullable 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 |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/android/database/Cursor.html data` | The value that was returned by `https://developer.android.com/reference/androidx/loader/content/AsyncTaskLoader#loadInBackground()`, or null if the task threw `https://developer.android.com/reference/androidx/core/os/OperationCanceledException`. |

### setProjection

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/loader#1.0.0)

```
public void setProjection(@Nullable String[] projection)
```

### setSelection

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/loader#1.0.0)

```
public void setSelection(@Nullable String selection)
```

### setSelectionArgs

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/loader#1.0.0)

```
public void setSelectionArgs(@Nullable String[] selectionArgs)
```

### setSortOrder

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/loader#1.0.0)

```
public void setSortOrder(@Nullable String sortOrder)
```

### setUri

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/loader#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 `https://developer.android.com/reference/androidx/loader/content/Loader#reset()`. This is not called by clients directly, but as a result of a call to `https://developer.android.com/reference/androidx/loader/content/Loader#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