Android: What’s new with Paging 3.0?

Tram Ho

Paging 3.0 is a major update from previous versions of Android’s Paging Architecture Component

Almost all apps show a large list of data to the user, but at a time the user sees only a small portion of the data in your app, so fetching all of the data from The network is not an effective solution. The solution is to fetch small pieces of data at a time, and as soon as the user reaches the bottom of the list it is easy to fetch more data. This is called Paging. With the release of Android 11 beta, Google also updated their Paging library. With Paging 3 it’s really simple and effective to implement into your Android app

What’s new with Paging 3.0?

Paging 3.0 is significantly different from previous versions. Some of the new features of Paging 3.0:

  • Support for Kotlin coroutines, Flow as with LiveData and Rxjava
  • Built-in support for handling error, refresh and retry features
  • Integrated support for loading state header, footer and list separators
  • In memory caching, ensure efficient use of system resources
  • Prevent duplicate api calls
  • Improvements to the repository, canceling support and simplified data source.


We just need to add the dependency to build.gradle

If you want to use more RxJava or LiveData you need to add more

Create Data Source

  • Unlike previous Paging versions, in Paging 3.0 we had to implement PagingSource <Key, Value> to define data source. PagingSource includes 2 parameters Key and Value. The key is a paramter that identifies the data to be loaded, such as the page number or data type

  • Note that the overridden load () function is a suspend function so we can request api here to get data from the network or in the database easily.
  • Object LoadParams contains information about fetch methods such as Key and Page size
  • If the api is successfully requested, then it will return a response data that is wrapped in the LoadResult.Page object along with the previous and next key.
  • If the api request fails, an exception wrapped in LoadResult.Error will be returned

The figure below shows exactly how the load () function gets the keys from the last fetch and provides the keys for the next fetch.

Get Paging data in ViewModel

Now we will create an instance of Pager in our viewmodel by creating a stream of data from MoviePagineSource.

  • Object Pager calls the load () method from MoviePagingSource, provides it with LoadParams and returns LoadResult
  • We also have to provide configs like the pagezie for PagingConfig
  • cacheIn (viewModelScope) stores the data from MoviePagingSource to exist with screen orentation change.

Displays on Recycler View

First we must create a RecyclerView Adapter extends class from PagingDataAdapter. It is like a normal RecyclerView Adapter. PagingDataAdapter takes two parameters, the first is the data type (in this case, Movie) and the second is RecyclerView.ViewHolder.

In OnActivityCreated () in your fragment, we can collect data from PagingData and then submit to MovieAdapter.

The RecyclerView currently displays data from the DataSource and automatically loadsmore when we scroll down to the bottom of the list

Display LoadState and Error Message

  • Paging 3.0 supports fetch status display and error handling. Paging displays the fetch state for use in the UI through the LoadState object.
  • If the LoadState is LoadState.NotLoading, then no fetch is inactive and no error
  • If LoadState is LoadState.Loading then the current state is fetching
  • If the LoadState is a LoadState.Error then it is in Error state

We can listen in the addLoadStateListener () method of MovieAdapter to listen for the updated LoadState.


Paging 3.0 is a major update from previous versions, and it is completely rewritten from previous versions of the Paging library. It has complete support for Couroutines and reative streams like RxJava and LiveData. It also has built-in error handling and load state management support that makes it easy to implement paging in your application.

Refer to

Share the news now

Source : Viblo