I implemented registerAdapterDataObserver for a Recycler object in a Fragment. I implemented this DataObserver so when data is added to the Recycler list, I can update a count that is displayed to the User.
baggageListAdapter.registerAdapterDataObserver(object : RecyclerView.AdapterDataObserver() {
override fun onChanged() {
super.onChanged()
setBaggageCount()
}
})
Should I be implementing unregisterAdapterDataObserver someplace in the Fragment. Like in
override fun onDestroy() {
super.onDestroy()
}
Since your Fragment will outlive the RecyclerView.Adapter, it is unnecessary to unregister the listener.
If your design is clean, I think it should be unnecessary to listen to adapter changes. You could be observing the same LiveData/Flow/Flowable that is providing data to the adapter, or if you're not using a reactive pattern, then you could be updating that count in the same function that's used to change the backing data. It's kind of awkward to be using a view object as source of truth about data state.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With