Should Item.unbind() on all items be called when the Activity/Fragment holding reference to the adapter is destroyed?
See original GitHub issueIām experiencing some unexpected issues with Items and cleaning up references when items should be unbound. But, maybe Iām not understanding how the unbind function is supposed to work.
If I set up listeners or RxJava subscriptions in bind(viewHolder, position)
of a custom Item class and I exit the activity which holds reference to the adapter containing these items, shouldnāt unbind(holder)
be called on all items?
Is there something specific I have to call before the activity/fragment is stopped?
In my particular case, unbind(holder)
is never called for any of the items in my adapter.
Iām happy to create a bug ticket if this turns out to be a bug.
Issue Analytics
- State:
- Created 4 years ago
- Comments:8 (4 by maintainers)
Top Results From Across the Web
support FragmentPagerAdapter holds reference to old fragments
You are running into a problem because you are instantiating and keeping references to your fragments outside of PagerAdapter.getItem , and are trying...
Read more >FragmentStateAdapter - Android Developers
The adapter will be responsible for the Fragment lifecycle: The Fragment will be used to display an item. The Fragment will be destroyed...
Read more >5 common mistakes when using Architecture Components
Fragments have tricky lifecycle and when a fragment gets detached and re-attached it is not always actually destroyed, for example, retainedĀ ...
Read more >MVP for Android: how to organize the presentation layer
The only thing that the view will do is calling a presenter method every ... Delivery system : Activity, Fragment, Service and other...
Read more >Handling Orientation Changes on Android - Medium
So instead of destroying and recreating your Activity, Android will just rotate the ... Android will call all of the other callbacks because...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Ok š Iāll give the LifecycleObserver option a shot and report back just so other folks can see a resolution. But, feel free to close the ticket.
Really appreciate your help!
unbind(holder)
is called whenever a ViewHolder is recycled (called fromRecyclerView.Adapter.onViewRecycled
method).Activities/Fragments do not explicitly alert RecyclerView adapters about lifecycle changes.
There is two things you can do here depending on your use case. One of them is to override the
onViewAttachedToWindow
andonViewDetachedFromWindow
functions inItem
. These are driven from the corresponding functions from the recyclerview adapter. But you will NOT get anonViewDetachedFromWindow
event when the activity/fragment is stopped or destroyed.What it sounds like to me is that your Items need to implement
LifecycleObserver
. And if your RecyclerView is used within an Activity it should be really simple to use:Things are a little bit tricker if you want to bind it to a Fragment lifecycle. Since the context of the
viewHolder.itemView
will ultimately be an Activity one. In that case youād need to pass the reference of theFragment
as aLifecycleOwner
to your items.Does this help?