0
0
Android Kotlinmobile~20 mins

Item keys for performance in Android Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
RecyclerView Key Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2: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?
AIt helps RecyclerView efficiently update only changed items, improving performance and animations.
BIt prevents the RecyclerView from displaying any items.
CIt disables item recycling, so views are never reused.
DIt automatically sorts the list items alphabetically without extra code.
Attempts:
2 left
💡 Hint
Think about how RecyclerView knows which items changed or moved.
📝 Syntax
intermediate
2: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()
    }
}
Areturn items.size.toLong()
Breturn position.toLong()
Creturn items[position].id.toLong()
Dreturn 0L
Attempts:
2 left
💡 Hint
Use a unique identifier from the item, not the position.
lifecycle
advanced
2: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?
ARecyclerView ignores getItemId() and animations may not work correctly.
BAnimations work perfectly because getItemId() is overridden.
CRecyclerView crashes at runtime.
DRecyclerView disables recycling of views.
Attempts:
2 left
💡 Hint
Think about the flag that tells RecyclerView to use stable IDs.
🔧 Debug
advanced
2: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?
AgetItemCount()
BgetItemId()
ConCreateViewHolder()
DonBindViewHolder()
Attempts:
2 left
💡 Hint
Stable IDs require both a flag and a method override.
🧠 Conceptual
expert
3: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?
AUse the list position as the item key.
BUse the message content string as the item key.
CUse the timestamp as the item key.
DUse a unique message ID assigned by the server as the item key.
Attempts:
2 left
💡 Hint
Keys must uniquely identify each item even if other fields are the same.