Challenge - 5 Problems
RecyclerView Key Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate2:00remaining
Why use stable item keys in a RecyclerView?
In Android development, why is it important to provide stable item keys when using a RecyclerView adapter?
Attempts:
2 left
💡 Hint
Think about how RecyclerView knows which items changed or moved.
✗ Incorrect
Stable keys let RecyclerView track items uniquely, so it can animate changes and avoid redrawing the whole list.
📝 Syntax
intermediate2:00remaining
Correct way to override getItemId() for stable keys
Which of the following Kotlin code snippets correctly overrides getItemId() in a RecyclerView.Adapter to provide stable item keys based on a unique item ID?
Android Kotlin
class MyAdapter(private val items: List<Item>) : RecyclerView.Adapter<MyViewHolder>() { init { setHasStableIds(true) } override fun getItemCount() = items.size override fun getItemId(position: Int): Long { return items[position].id.toLong() } }
Attempts:
2 left
💡 Hint
Use a unique identifier from the item, not the position.
✗ Incorrect
Using a unique item ID ensures stable keys even if the list order changes.
❓ lifecycle
advanced2:00remaining
Effect of missing stable IDs on RecyclerView animations
What happens to RecyclerView item animations if you do NOT set setHasStableIds(true) but override getItemId() with unique IDs?
Attempts:
2 left
💡 Hint
Think about the flag that tells RecyclerView to use stable IDs.
✗ Incorrect
Without setHasStableIds(true), RecyclerView does not use getItemId() for animations.
🔧 Debug
advanced2:00remaining
Why does RecyclerView show wrong data after list update?
You updated your list data and called notifyDataSetChanged(), but RecyclerView shows wrong items or flickers. You use stable IDs but forgot to override which method?
Attempts:
2 left
💡 Hint
Stable IDs require both a flag and a method override.
✗ Incorrect
Overriding getItemId() provides unique IDs; without it, stable IDs don't work properly.
🧠 Conceptual
expert3:00remaining
Choosing item keys for complex data with duplicates
You have a list of chat messages where some messages have identical content and timestamps. Which is the best approach to provide stable keys for RecyclerView items to ensure correct UI updates?
Attempts:
2 left
💡 Hint
Keys must uniquely identify each item even if other fields are the same.
✗ Incorrect
Only a unique server-assigned ID guarantees stable keys for identical content items.