airbnb / epoxy

Epoxy is an Android library for building complex screens in a RecyclerView
https://goo.gl/eIK82p
Apache License 2.0
8.46k stars 730 forks source link

EpoxyRecyclerView.withModels finishes after Fragment's View is destroyed #1298

Open tomislavturcic opened 2 years ago

tomislavturcic commented 2 years ago

I'm seeing Firebase crashes in a couple of Fragments that are using EpoxyRecyclerView.withModels {...}

Stacktrace is usually as follows:

Fatal Exception: java.lang.IllegalStateException
Fragment d{4cc615e} (3fe66274-2caa-4a54-9698-137ed7497410) not attached to a context.

Code:

val epoxyRecyclerView = requireView() as EpoxyRecyclerView
epoxyRecyclerView.withModels {
    someItemView {
            id(item.id)
            item(item)
            text(getString(R.string.some_string))   // <--- this crashes
            onItemClickListener { model, _, _, _ ->
                ...
            }
        }
}

I assume calling epoxyRecyclerView.clear() in Fragment's onDestroyView() will fix this issue, but that means I need to look-up a dozen Fragments and add this manually. Is there a better way?

Epoxy Version: '4.6.4'