0Pricing
Android Academy · Lesson

Why Paging

The cost of loading everything at once.

Why Paging is a free Android Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Android Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Loading Everything Is Expensive

Imagine a feed with 50,000 items. If you fetch and hold them all at once, you waste memory, slow down the network, and freeze the UI while the response is parsed.

Most users only ever scroll through the first few screens. Paging means loading data in small chunks (pages) as the user scrolls, instead of all upfront.

The Problem in Code

A naive approach loads the full list into memory. Even if the server supports it, the response can be huge and the parsed list dominates the heap.

This pattern does not scale and risks OutOfMemoryError on large datasets.

// Anti-pattern: load the whole table at once
suspend fun loadAllArticles(): List<Article> {
    // Could be tens of thousands of rows / megabytes of JSON
    return api.getArticles(limit = 50_000)
}

// UI holds ALL of them in memory at once
val articles = loadAllArticles()
LazyColumn {
    items(articles) { article -> ArticleRow(article) }
}

What a Page Looks Like

A page is a small slice of the dataset, often 20-50 items. The server returns one page plus a pointer to the next page (a number, offset, or token).

The app keeps only a few pages in memory and discards old ones as the user scrolls away.

data class ArticlePage(
    val items: List<Article>,
    val nextKey: Int?   // null means no more pages
)

// Example REST call returning one page
suspend fun getArticlePage(page: Int, size: Int = 20): ArticlePage

Meet Paging 3

Paging 3 is the Jetpack library that handles all the hard parts of paging for you:

  • Requesting the next page when the user nears the end of the list
  • Keeping only a window of items in memory
  • Exposing loading and error states
  • Built-in support for coroutines, Flow and Jetpack Compose

Adding the Dependency

Paging 3 ships as separate artifacts: a runtime plus a Compose integration. Add them in your module's build.gradle.kts.

The paging-compose artifact gives you helpers for LazyColumn.

// build.gradle.kts (module)
dependencies {
    val pagingVersion = "3.3.6"
    implementation("androidx.paging:paging-runtime:$pagingVersion")
    implementation("androidx.paging:paging-compose:$pagingVersion")
}

The Three Core Pieces

Paging 3 revolves around three types you will use again and again:

  • PagingSource - knows how to load one page from a source
  • Pager - configures and produces a stream of paged data
  • PagingData - the container of items, emitted as a Flow

We will build each of these in the next lessons.

PagingData and the Flow

Your repository exposes a Flow<PagingData<T>>. The UI collects it and renders whatever items are currently loaded.

You never assemble the full list yourself - Paging streams items into the UI on demand.

import androidx.paging.Pager
import androidx.paging.PagingConfig
import androidx.paging.PagingData
import kotlinx.coroutines.flow.Flow

class ArticleRepository(private val api: ArticleApi) {
    fun articles(): Flow<PagingData<Article>> = Pager(
        config = PagingConfig(pageSize = 20)
    ) {
        ArticlePagingSource(api)
    }.flow
}

PagingConfig Controls the Window

PagingConfig tunes how Paging loads and holds data:

  • pageSize - items per page request
  • prefetchDistance - how far from the edge to trigger the next load
  • enablePlaceholders - show placeholder slots for not-yet-loaded items
  • maxSize - cap on items kept in memory
import androidx.paging.PagingConfig

val config = PagingConfig(
    pageSize = 20,
    prefetchDistance = 5,
    enablePlaceholders = false,
    maxSize = 100
)

Network or Database - or Both

A PagingSource can pull from a REST API, a Room database, or any custom source.

For a true offline-first experience you combine the two with a RemoteMediator (covered in the last lesson): the network fills a local database, and the UI pages from that database.

Smooth Scrolling, Less Memory

With paging, scrolling stays smooth because each request is tiny and the in-memory window is bounded by maxSize.

Loading and error indicators come for free through LoadState, so you can show a spinner at the bottom or a retry button without manual bookkeeping.

// LoadState exposes loading/error per direction
when (val state = adapterLoadState.append) {
    is LoadState.Loading -> showBottomSpinner()
    is LoadState.Error -> showRetry(state.error)
    is LoadState.NotLoading -> hideBottomSpinner()
}

When You Need Paging

Reach for Paging 3 when:

  • The dataset is large or unbounded (feeds, search results, chat history)
  • Data comes from a network API that returns pages or cursors
  • You want automatic loading/error UI and memory control

For a short, fixed list (a settings menu), a plain LazyColumn is simpler and perfectly fine.

Quick Check

What is the main benefit of using Paging 3 for a large list?

Recap: Why Paging

You learned why loading everything at once hurts performance and memory, and how Paging 3 solves it.

  • Data is loaded in small pages as the user scrolls
  • The three core pieces are PagingSource, Pager and PagingData
  • PagingConfig tunes page size, prefetch and the memory window
  • Paging integrates with coroutines, Flow and Compose, and gives loading/error states for free

Next: building a PagingSource and wiring it into a Pager.

Frequently asked questions

Is the “Why Paging” lesson free?

Yes — the full text of “Why Paging” is free to read here on the web, and the Android Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Android Academy course, upgrade to CoddyKit PRO.

What will I learn in “Why Paging”?

The cost of loading everything at once. You practise Android Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Android Academy?

No prior experience is required. Android Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Why Paging” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Android Academy lesson?

Yes. Every Android Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Why Paging
  2. PagingSource and Pager
  3. Paging in Compose Lists
  4. RemoteMediator and Caching
← Back to Android Academy