androidbroadcast / ViewBindingPropertyDelegate

Make work with Android View Binding simpler
https://proandroiddev.com/make-android-view-binding-great-with-kotlin-b71dd9c87719
Apache License 2.0
1.42k stars 102 forks source link

OnViewDestroyed is called multiple times even before view get destroyed #77

Closed OmarELRayes closed 2 years ago

OmarELRayes commented 2 years ago

I have 3 Fragments, Splash->Introduction->Login using Single Activity Architecture. the IntroductionFragmentgot a ViewPager2 and LeakCanary shows a memory leak because of the adapter so I have to set it to be null in OnDestoyView() so here is the code I use

private val binding by viewBinding(FragmentGetStartedBinding::bind,
    onViewDestroyed = {
        it.imagesViewPager.adapter = null
    })

however what happens that the view pager shows the images then 'OnViewDestroyed' gets called from your library clearing the adapter before leaving the fragment

GIF showcasing the behavior : https://gyazo.com/fdf0e31fdb9a9ae4bd64714c42d1ce17

OmarELRayes commented 2 years ago

That's what I'm currently using to avoid the memory leak.

 override fun onDestroyView() {
        //because of ViewPager2's adapter memory leak, can't be accessed via binding at this state
        view?.findViewById<ViewPager2>(R.id.imagesViewPager)?.adapter = null
        super.onDestroyView()
    }
E-D-W-I-N commented 2 years ago

Yeah, I've faced same issue as well. I have two fragments (list of items and details page), let's call them A and B. When I navigate A -> B onViewDestroyed of A fragment is called as intended. But when I do popBackStack inside B fragment and navigate to A fragment again onViewDestroyed of A fragment is called right after onViewCreated

OmarELRayes commented 2 years ago

have the issue been solved ?

kirich1409 commented 2 years ago

I hope yes. The fix will be in 1.5.2. The artefacts are processing by Maven Central right now