question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItĀ collects links to all the places you might be looking at while hunting down a tough bug.

And, if youā€™re still stuck at the end, weā€™re happy to hop on a call to see how we can help out.

Should Item.unbind() on all items be called when the Activity/Fragment holding reference to the adapter is destroyed?

See original GitHub issue

Iā€™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:closed
  • Created 4 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
TylerMcCrawcommented, Nov 21, 2019

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!

1reaction
ValCanBuildcommented, Nov 21, 2019

unbind(holder) is called whenever a ViewHolder is recycled (called from RecyclerView.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 and onViewDetachedFromWindow functions in Item. These are driven from the corresponding functions from the recyclerview adapter. But you will NOT get an onViewDetachedFromWindow 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:

class MyItem() : Item(), LifecycleObserver {

    override fun bind(viewHolder: GroupieViewHolder, position: Int) {
        (viewHolder.itemView.context as AppCompatActivity).lifecycle.addObserver(this)
    }

    override fun unbind(viewHolder: GroupieViewHolder) {
        super.unbind(viewHolder)
        (viewHolder.itemView.context as AppCompatActivity).lifecycle.removeObserver(this)
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
    fun onStop() {
        //perform onStopLogic
    }
}

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 the Fragment as a LifecycleOwner to your items.

Does this help?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found