tezvyn:

How to implement efficient large lists in Jetpack Compose?

Curated by the Tezvyn teamSource: developer.android.comintermediate
How to implement efficient large lists in Jetpack Compose?

This tests your grasp of Compose performance. Use `LazyColumn` to compose only visible items, unlike a `Column` which renders all items at once. A red flag is suggesting a scrollable `Column`, which has severe performance costs for large lists.

WHAT THIS TESTS: This question tests your understanding of performance optimization in Compose. It's not just about knowing the name of a composable, but about understanding the fundamental difference between composing all UI elements at once versus composing them on demand. The interviewer is looking for your grasp of how declarative UI handles resource management, specifically for lists with a large or unknown number of items.

A GOOD ANSWER COVERS: An excellent answer addresses four points in order. First, state the correct tool for the job: LazyColumn for vertical lists, LazyRow for horizontal, or LazyVerticalGrid for grids. Second, explain the core mechanism: these composables are 'lazy' because they only compose and measure the items currently visible in the viewport, plus a small buffer for smooth scrolling. Third, contrast this directly with a standard Column or Row. Using a Column with a forEach loop composes every single item in the list upfront, leading to high memory usage and potentially long initial load times that can cause an Application Not Responding (ANR) error. Fourth, mention advanced but crucial features like providing a stable key for each item to help Compose optimize for data changes (adds, moves, deletes) and using LazyListState to observe or control the scroll position programmatically.

COMMON WRONG ANSWERS: The most common red flag is suggesting a Column wrapped in a Modifier.verticalScroll(). This is a performance trap. It makes the content scrollable but still composes every item in the list at once, defeating the purpose of lazy loading. Another weak answer is simply naming LazyColumn without explaining the 'why'—the on-demand composition principle. Forgetting to mention the importance of the key parameter is a missed opportunity to demonstrate senior-level knowledge, as it's critical for performance in dynamic lists.

LIKELY FOLLOW-UPS: Expect questions like: "How would you handle different view types within a single LazyColumn?" (Answer: use the item block with conditional logic or the contentType parameter). "When is it appropriate to use a scrollable Column?" (Answer: for a small, fixed number of items where the performance overhead is negligible). "How would you save and restore the scroll position across configuration changes?" (Answer: use rememberLazyListState with rememberSaveable).

ONE CONCRETE EXAMPLE: Imagine a list with 5,000 items. A Column would attempt to create and hold all 5,000 item composables in memory at once, likely causing a multi-second freeze or an ANR on a mid-range device. A LazyColumn, however, would only create and measure the 8-10 items visible on screen, plus a few offscreen. Its initial composition time would be milliseconds, and its memory usage would be a tiny fraction of the Column's, regardless of whether the list has 5,000 or 500,000 items.

Source: developer.android.com/jetpack/compose/lists

Read the original → developer.android.com

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.

How to implement efficient large lists in Jetpack Compose? · Tezvyn