Home > Back-end >  Recycler unregisterAdapterDataObserver
Recycler unregisterAdapterDataObserver

Time:01-14

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

    }

CodePudding user response:

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.

  •  Tags:  
  • Related