Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recycler unregisterAdapterDataObserver

Tags:

android

kotlin

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()

    }
like image 234
Beachdog Avatar asked Mar 29 '26 16:03

Beachdog


1 Answers

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.

like image 185
Tenfour04 Avatar answered Mar 31 '26 05:03

Tenfour04