Does Jetpack Compose automatically generate keys for LazyColumn items?
0 reputation · 13 Nov 2025, 20:04 UTC
0 reputation · 13 Nov 2025, 20:04 UTC
I am working with Jetpack Compose 1.4 or later and need to understand whether the framework can infer stable keys for items in a LazyColumn or LazyRow when the underlying list is reordered or filtered. The documentation indicates that explicit keys are required to avoid unintended recompositions, but it is unclear whether any automatic key inference exists for common data types or whether the responsibility always rests with the caller.
This matters because missing keys can cause excessive recomposition and may interfere with state restoration across configuration changes or back-stack navigation. I am looking for confirmation of the current contract and any version-specific exceptions before deciding whether to add keys manually.
Does Compose generate implicit keys for Lazy list items, or must every item key be supplied explicitly? Are there collection types or item layouts where automatic keying is safe to rely on?
No, Jetpack Compose does not automatically generate stable, content-based keys for LazyColumn or LazyRow items. If you do not provide an explicit key, the framework defaults to using the item's index in the list as its implicit key.
Because the default key is the position (index), Compose identifies items by where they are, not what they are. This leads to specific behaviors when the underlying data changes:
rememberSaveable value or a TextField focus) tied to a specific index will remain at that index even if the data associated with that index changes.To maintain item identity across reorders or filters, you must provide a unique, stable identifier from your data model via the key parameter in the items() function. This is the standard contract for all versions of Compose, including 1.4+.
val users = listOf(User(id = "a1", name = "Alice"), User(id = "b2", name = "Bob"))
LazyColumn {
items(
items = users,
key = { user -> user.id } // Explicit stable key
) {
UserRow(user)
}
}
You can verify the necessity of explicit keys with this test:
LazyColumn where each item contains a TextField.This behavior assumes you are using the standard androidx.compose.foundation.lazy package. Note that keys must be unique within the list; providing duplicate keys will result in a runtime exception. To provide a more tailored recommendation, please specify if your data source is a static list or a reactive stream (like Flow or LiveData).
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.