Google is committed to advancing racial equity for Black communities. See how.


open class ContentPager
   ↳ androidx.contentpager.content.ContentPager

ContentPager provides support for loading "paged" data on a background thread using the ContentResolver framework. This provides an effective compatibility layer for the ContentResolver "paging" support added in Android O. Those Android O changes, like this class, help reduce or eliminate the occurrence of expensive inter-process shared memory operations (aka "CursorWindow swaps") happening on the UI thread when working with remote providers.

The list of terms used in this document:

    "The provider" is a android.content.ContentProvider supplying data identified by a specific content Uri. A provider is the source of data, and for the sake of this documents, the provider resides in a remote process.
      "supports paging" A provider supports paging when it returns a pre-paged Cursor that honors the paging contract. See @link ContentResolver#QUERY_ARG_OFFSET} and ContentResolver#QUERY_ARG_LIMIT for details on the contract.
        "CursorWindow swaps" The process by which new data is loaded into a shared memory via a CursorWindow instance. This is a prominent contributor to UI jank in applications that use Cursor as backing data for UI elements like RecyclerView.


        Data will be loaded from a content uri in one of two ways, depending on the runtime environment and if the provider supports paging.

      1. If the system is Android O and greater and the provider supports paging, the Cursor will be returned, effectively unmodified, to a ContentCallback supplied by your application.
      2. If the system is less than Android O or the provider does not support paging, the loader will fetch an unpaged Cursor from the provider. The unpaged Cursor will be held by the ContentPager, and data will be copied into a new cursor in a background thread. The new cursor will be returned to a ContentCallback supplied by your application.

        In either cases, when an application employs this library it can generally assume that there will be no CursorWindow swap. But picking the right limit for records can help reduce or even eliminate some heavy lifting done to guard against swaps.

        How do we avoid that entirely?

        Picking a reasonable item limit

        Authors are encouraged to experiment with limits using real data and the widest column projection they'll use in their app. The total number of records that will fit into shared memory varies depending on multiple factors.

      3. The number of columns being requested in the cursor projection. Limit the number of columns, to reduce the size of each row.
      4. The size of the data in each column.
      5. the Cursor type.

        If the cursor is running in-process, there may be no need for paging. Depending on the Cursor implementation chosen there may be no shared memory/CursorWindow in use. NOTE: If the provider is running in your process, you should implement paging support inorder to make your app run fast and to consume the fewest resources possible.

        In common cases where there is a low volume (in the hundreds) of records in the dataset being queried, all of the data should easily fit in shared memory. A debugger can be handy to understand with greater accuracy how many results can fit in shared memory. Inspect the Cursor object returned from a call to ContentResolver#query(Uri, String[], String, String[], String). If the underlying type is a android.database.CrossProcessCursor or android.database.AbstractWindowedCursor it'll have a CursorWindow field. Check CursorWindow#getNumRows(). If getNumRows returns less than Cursor#getCount, then you've found something close to the max rows that'll fit in a page. If the data in row is expected to be relatively stable in size, reduce row count by 15-20% to get a reasonable max page size.

        What if the limit I guessed was wrong?

        The library includes safeguards that protect against situations where an author specifies a record limit that exceeds the number of rows accessible without a CursorWindow swap. In such a circumstance, the Cursor will be adapted to report a count ({Cursor#getCount}) that reflects only records available without CursorWindow swap. But this involves extra work that can be eliminated with a correct limit.

        In addition to adjusted coujnt, EXTRA_SUGGESTED_LIMIT will be included in cursor extras. When EXTRA_SUGGESTED_LIMIT is present in extras, the client should strongly consider using this value as the limit for subsequent queries as doing so should help avoid the ned to wrap pre-paged cursors.

        Lifecycle and cleanup

        Cursors resulting from queries are owned by the requesting client. So they must be closed by the client at the appropriate time.

        However, the library retains an internal cache of content that needs to be cleaned up. In order to cleanup, call reset().


        Note that projection is ignored when determining the identity of a query. When adding or removing projection, clients should call reset() to clear cached data.


Nested classes

Callback by which a client receives results of a query.


Implementations of this interface provide the mechanism for execution of queries off the UI thread.

static Int

The cursor size exceeded page size.

static Int

The cursor was provider paged.

static Int

The cursor was pre-paged, but total size was larger than CursorWindow size.

static Int

The cursor was not pre-paged, but total size was smaller than page size.

static String

static String

Denotes the requested limit, if the limit was not-honored.

static String

Specifies a limit likely to fit in CursorWindow limit.

static String

static String

static String

Public constructors
<init>(resolver: ContentResolver!, queryRunner: ContentPager.QueryRunner!)

Creates a new ContentPager with a default cursor cache size of 1.

<init>(@NonNull resolver: ContentResolver, @NonNull queryRunner: ContentPager.QueryRunner, cursorCacheSize: Int)

Creates a new ContentPager.

Public methods
open static Bundle
createArgs(offset: Int, limit: Int)

Builds a Bundle with offset and limit values suitable for with query(Uri, String[], Bundle, CancellationSignal, ContentCallback).

open Query
query(@NonNull uri: Uri, @Nullable projection: Array<String!>?, @NonNull queryArgs: Bundle, @Nullable cancellationSignal: CancellationSignal?, @NonNull callback: ContentPager.ContentCallback)

Initiates loading of content.

open Unit

Clears any cached data.




The cursor size exceeded page size. A new cursor with with page data was created.

Value: 1



The cursor was provider paged.

Value: 2



The cursor was pre-paged, but total size was larger than CursorWindow size.

Value: 3



The cursor was not pre-paged, but total size was smaller than page size. Cursor wrapped to supply data in extras only.

Value: 4


static val EXTRA_HONORED_ARGS: String
Value: ContentResolver.EXTRA_HONORED_ARGS


static val EXTRA_REQUESTED_LIMIT: String

Denotes the requested limit, if the limit was not-honored.

Value: "android-support:extra-ignored-limit"


static val EXTRA_SUGGESTED_LIMIT: String

Specifies a limit likely to fit in CursorWindow limit.

Value: "android-support:extra-suggested-limit"


static val EXTRA_TOTAL_COUNT: String
Value: ContentResolver.EXTRA_TOTAL_COUNT


static val QUERY_ARG_LIMIT: String
Value: ContentResolver.QUERY_ARG_LIMIT


static val QUERY_ARG_OFFSET: String
Value: ContentResolver.QUERY_ARG_OFFSET

Public constructors


    resolver: ContentResolver!,
    queryRunner: ContentPager.QueryRunner!)

Creates a new ContentPager with a default cursor cache size of 1.


    @NonNull resolver: ContentResolver,
    @NonNull queryRunner: ContentPager.QueryRunner,
    cursorCacheSize: Int)

Creates a new ContentPager.

cursorCacheSize Int: Specifies the size of the unpaged cursor cache. If you will only be querying a single content Uri, 1 is sufficient. If you wish to use a single ContentPager for queries against several independent Uris this number should be increased to reflect that. Remember that adding or modifying a query argument creates a new Uri.
resolver ContentResolver: The content resolver to use when performing queries.
queryRunner ContentPager.QueryRunner: The query running to use. This provides a means of executing queries on a background thread.

Public methods


@NonNull open static fun createArgs(
    offset: Int,
    limit: Int
): Bundle

Builds a Bundle with offset and limit values suitable for with query(Uri, String[], Bundle, CancellationSignal, ContentCallback).

offset Int: must be greater than or equal to 0.
limit Int: can be any value. Only values greater than or equal to 0 are respected. If any other value results in no upper limit on results. Note that a well behaved client should probably supply a reasonable limit. See class documentation on how to select a limit.
Bundle Mutable Bundle pre-populated with offset and limits vales.


@MainThread @NonNull open fun query(
    @NonNull uri: Uri,
    @Nullable projection: Array<String!>?,
    @NonNull queryArgs: Bundle,
    @Nullable cancellationSignal: CancellationSignal?,
    @NonNull callback: ContentPager.ContentCallback
): Query

Initiates loading of content. For details on all params but callback, see ContentResolver#query(Uri, String[], Bundle, CancellationSignal).

uri Uri: The URI, using the content:// scheme, for the content to retrieve.
projection Array<String!>?: A list of which columns to return. Passing null will return the default project as determined by the provider. This can be inefficient, so it is best to supply a projection.
queryArgs Bundle: A Bundle containing any arguments to the query.