In Jetpack Compose, a LazyColumn of AndroidView-hosted lyric views stutters on scroll, and after adding onReset some rows show another song's lyrics; why, and how do you fix both?
answer
- new View for every item
- reuse is opt-in
- non-null onReset enables it
- per-item data set once in factory
- onReset before update; onRelease is final
basics
~20 sWithout a non-null onReset, AndroidView discards and recreates its View whenever the item leaves, so scrolling inflates constantly. With onReset, Views are reused, and data set in factory goes stale; move per-item data into update.
solid answer
~50 sBy default `AndroidView` does not reuse Views: in a `LazyColumn` each item that scrolls out discards its View and each new item calls `factory` again, which is expensive and causes jank. Passing a non-null `onReset` opts in to reuse, so Compose can hand an existing View to a compatible item with the same composable group structure. Reuse breaks code that sets per-item data in `factory`, because `factory` ran only for the first item that View served. The fix is to keep `factory` item-independent and set the song's lyrics in `update`, which runs again for the new item. Use `onReset` to clear transient state such as scroll position or a running animation; it is invoked on the UI thread before `update`. Use `onRelease` to free resources when the View leaves the composition for good, after which it is never reused.
code
kotlin · 10 linesLazyColumn {
items(songs) { song ->
AndroidView(
factory = { context -> LyricsView(context) },
update = { view -> view.setLyrics(song.lyrics) },
onReset = { view -> view.scrollTo(0, 0) },
onRelease = { view -> view.stopPreview() },
)
}
}go deeper
Know that AndroidView can sit in a LazyColumn and that the overload with onReset lets Compose reuse the hosted View between items.
Explain that reuse needs a non-null onReset, that onReset runs before update, and that onRelease marks the end of a View's life.
Diagnose scroll jank from constant View creation and stale rows from data set in factory, and move binding into update with cleanup in onReset.
Treat AndroidView in lists as a migration cost: measure scroll performance, require onReset for list usage in review, and schedule hot rows for rewriting in Compose.
## Two symptoms, one mechanism Picture a karaoke-booking app whose song list is a `LazyColumn`, with each row hosting a legacy `LyricsView` through `AndroidView`. Two complaints arrive one after the other: 1. Scrolling stutters. 2. After someone adds `onReset` to fix the stutter, some rows show another song's lyrics. Both come from how `AndroidView` handles **View reuse** in lazy layouts. ## Why the list stutters The `AndroidView` overload without `onReset`, or with `onReset = null`, **does not pool or reuse Views**. The documentation is explicit: inside a reusable container such as `LazyRow` or `LazyColumn`, the View instances are always discarded and recreated when the composition hierarchy changes, even when the View could have been reused. Lazy lists compose items as they scroll in and dispose them as they scroll out, so every new row runs `factory` again: a full View construction or inflation on the UI thread, item after item. That is the jank. ## Opting in to reuse The overload `AndroidView(factory, modifier, onReset, onRelease, update)` makes Views **eligible for reuse when `onReset` is non-null**. The `onReset` and `onRelease` parameters have been there since Compose 1.4.0-rc01, and Compose 1.12 keeps them. Two `AndroidView` calls are **compatible** when they are invoked with the same composable group structure, and lazy layouts reuse layout nodes, and therefore Views, between such items as you scroll. The callbacks, all invoked on the UI thread: | Callback | When it runs | Use it for | |---|---|---| | `factory` | once per View instance | construction and item-independent setup | | `onReset` | when the View is about to be reused in a different context; before the next `update` | clearing transient state: scroll offset, animations, user input | | `update` | after `factory`, on recomposition with new values, when a `State` read in it changes, and again after reuse | binding this item's data | | `onRelease` | when the View leaves the composition permanently | freeing resources such as players or listeners | The documentation adds two details. `onReset` **may not be immediately followed by `update`**, because Compose can deactivate a View, detach it from its parent and keep it for later. After `onRelease` returns, Compose never reuses that instance; a later item gets a fresh View starting with `factory`. ## Why rows show the wrong lyrics The original code built the View with the data inside `factory`: ```kotlin factory = { context -> LyricsView(context).apply { setLyrics(song.lyrics) } } ``` Before reuse that looked correct, because every row got its own fresh View. Once Views are reused, a `LyricsView` created for song 3 can be handed to song 27. `factory` does not run again, so unless something rebinds it, the View still shows song 3. The fix: - keep `factory` **item-independent**: construct the View and attach listeners only; - set `setLyrics(song.lyrics)` in **`update`**, which runs for the new item after `onReset`; - clear leftovers from the previous item, such as the scroll position inside the lyrics, in **`onReset`**; - stop anything expensive, such as a preview player, in **`onRelease`**. ## Other things worth checking - Keep the item content's structure stable. Reuse needs compatible group structure, so wrapping the `AndroidView` in conditionals that differ per row reduces what can be reused. - If the View needs to know whether it is currently on screen, observe attachment with `View.addOnAttachStateChangeListener`; its `findViewTreeLifecycleOwner()` matches the composition's `LocalLifecycleOwner` once attached. - `AndroidViewBinding` has the same `onReset` and `onRelease` overload for XML-based rows. ## Review checklist for an AndroidView in a list 1. Is `onReset` non-null, so the View can be reused at all? 2. Does `factory` touch only things that are the same for every item? 3. Does `update` set **every** field that differs per item, including ones that are sometimes empty, so a reused View never keeps a previous value? 4. Does `onReset` clear transient state that `update` does not set, such as scroll offsets, running animations or text the user typed? 5. Does `onRelease` free what the View holds: players, listeners registered elsewhere, bitmaps? Point 3 catches a subtle variant of the stale-row bug: an `update` that sets the lyrics only when the song has them leaves the previous song's lyrics in place for a song that has none. Write `update` so it sets each property unconditionally, clearing it when there is no value. ## Takeaway Reuse is **opt-in through a non-null `onReset`**, and once it is on, `factory` is for building and `update` is for binding, exactly the split a `RecyclerView` adapter draws between creating and binding a view holder.
- After adding onReset, can a View stay off screen without update being called right away?Yes. The documentation says `onReset` may not be immediately followed by `update`: Compose can deactivate the View, remove it from its parent and retain it for possible reuse, for example in an inactive `ReusableContentHost` or while movable content is moved. If it is discarded from that state instead, it is released.
- What is the difference between onReset and onRelease for an AndroidView in a lazy list?`onReset` signals that the View is about to serve a new context and should drop transient state; the View lives on. `onRelease` signals that the View has left the composition permanently and will never be reused, so resources it holds should be freed. A later item that needs a View gets a new instance from `factory`.
- Does passing onReset guarantee that every scrolled-in row reuses a View?No. It makes the View eligible for reuse; Compose reuses it only for a compatible `AndroidView` invoked with the same composable group structure, which lazy layouts provide when item content is structured the same way. Rows whose structure differs still create new Views.
A karaoke bar hands one microphone to singer after singer. Wiping it before the next singer is onReset, loading the new singer's song on the screen is update, and retiring a broken microphone for good is onRelease. Loading the song only when the microphone was first unboxed would leave every later singer with the first singer's track.
- AndroidView reuses its View in a LazyColumn by default
- onRelease is what enables View reuse in lazy lists
- Setting per-item data in factory is safe once reuse is enabled
- onReset runs after update, so update must undo reset work
- A released View can still be reused by a later item
Assumes Compose UI 1.12; the AndroidView overload with onReset and onRelease has existed since 1.4.0-rc01 and is unchanged in 1.12.