Open PierreFourreau opened 4 years ago
Same problem here
When using a Navigation Graph which loads a contain fragment which contains FragmentStateAdapter
Its also reported here: https://issuetracker.google.com/issues/154751401
I had the same problem but it looks like I have found a workaround. In my case, I was adding data to the adapter before attaching it to the ViewPager2 adapter. If I first set the ViewPager2 adapter and then add data then looks like the FragmentStateAdapter is no longer leaking and the app is not crashing
The same problem.
My solution:
viewPager2.addOnAttachStateChangeListener(object : View.OnAttachStateChangeListener { override fun onViewDetachedFromWindow(v: View) { viewPager2.removeOnAttachStateChangeListener(this) tabLayoutMediator.detach() viewPager2.setup.unregisterOnPageChangeCallback(onPageChangeCallback) viewPager2..adapter = null } })
FragmentStateAdapter is still leaking because it uses lifecycle from mainnavigationfragment. But it uses only 800bytes.
Using viewpager.adapter = null
in onDestroyView()
is causing this issue, removing this line avoid the crash.
But nullify the ViewPager's adapter is an attempt to fix the memory leak which should be fixed with a yet unknown efficient manner 😞
You can try to set this layout property for androidx.viewpager2.widget.ViewPager2
android:saveEnabled="false"
Just create adapter with Fragment: FragmentStateAdapter(fragment)
Don't use FragmentStateAdapter(fragmentActivity)
You can try to set this layout property for androidx.viewpager2.widget.ViewPager2
android:saveEnabled="false"
U are my lifesaver! my viewpager was already inside a fragment, and I try the childFragmentManager but the app will crash when I press again, but adding this"savedEnabled=false", it works perfectly!
According to https://issuetracker.google.com/issues/151212195#comment3
FragmentStateAdapter(fragment.childFragmentManager, fragment.viewLifecycleOwner.lifecycle)
Has someone found a fix for this issue yet? I have tried quite a few things and only android:saveEnabled="false
seems to work, but I need the ViewPager2 to remember the page it is on
Has someone found a fix for this issue yet? I have tried quite a few things and only
android:saveEnabled="false
seems to work, but I need the ViewPager2 to remember the page it is on
Try to use FragmentStateAdapter(fragment, lifecycle)
@alkocher thanks, but isn't it same as using FragmentStateAdapter(fragment), because I will be anyways using the host fragments childFragmentManager and hostFragment's lifeCycle. Maybe I am missing something here?
@alkocher thanks, but isn't it same as using FragmentStateAdapter(fragment), because I will be anyways using the host fragments childFragmentManager and hostFragment's lifeCycle. Maybe I am missing something here?
I use this constructor with fragment.viewLifecycleOwner.lifecycle and set adapter to null when onDestroyView.
@alkocher thanks, will try this out 🙌
You can try to set this layout property for androidx.viewpager2.widget.ViewPager2
android:saveEnabled="false"
brooooo you saved my time and nerves today!
You can try to set this layout property for androidx.viewpager2.widget.ViewPager2
android:saveEnabled="false"
Thank you for this . Finally, I can fix it.
Just create adapter with Fragment: FragmentStateAdapter(fragment)
Don't use FragmentStateAdapter(fragmentActivity)
It didn't help in my case(
You can try to set this layout property for androidx.viewpager2.widget.ViewPager2
android:saveEnabled="false"
Thanks!
Hi,
I implement a basic viewpager2
My main fragment (which contain a viewpager of 2 fragments (fragmentA and fragment B))
Adapter :
The FragmentStateAdapter is leaking.
Like my others screens without viewpagers I tried
But it crash when I go to another fragment destination from fragmentA (or B) and come back :
Can you help? Thanks