I am using RecyclerView in my android project and its performance has been really awful. From the answers here, I tried adding adapter.setHasStableIds(true); to
In my case, it's because the I put the setHasStableIds(true) inside the Fragment's onActivityCreated(). The Fragment is the main point of my application so it never gets popped from back stack.
class LocalFragment: Fragment() {
private val adapter = LocalAdapter()
override fun onActivityCreated(savedInstanceState: Bundle?) {
adapter.setHasStableIds(true)
}
}
The problem here is that the onActivityCreated() is called every time the Fragment is navigated to. However, as the Fragment is still alive in the back stack, the private val adapter field is still the same object that had its setStableIds(true) called previously.
class LocalFragment: Fragment() {
private var adapter = LocalAdapter()
override fun onActivityCreated(savedInstanceState: Bundle?) {
adapter = LocalAdapter() // Re-assign the adapter
adapter.setHasStableIds(true)
}
}
Solution is to reassign the RecyclerView.Adapter object every time the fragment is switched to.