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.

Viewpager2 FragmentStateAdapter leak

See original GitHub issue

Hi,

I implement a basic viewpager2

My main fragment (which contain a viewpager of 2 fragments (fragmentA and fragment B))

...
 override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
 my_view_pager.adapter = MyAdapter(activity, args.refBrand)
        TabLayoutMediator(my_tab_layout, view_pager) { tab, position ->
            tab.text = if(position == 0) "1" else "2"
        }.attach()
}
...

Adapter :

class MyAdapter : FragmentStateAdapter(fragmentActivity) {

    override fun getItemCount(): Int = 2

    override fun createFragment(position: Int): Fragment = when (position) {
        0 -> FirstFragment.newInstance(filter = true)
        1 -> Second.newInstance(filter = false)
        else -> throw IllegalArgumentException...
    }
}

The FragmentStateAdapter is leaking.

Like my others screens without viewpagers I tried

override fun onDestroyView() {
        super.onDestroyView()
        my_view_pager.adapter = null
    }

But it crash when I go to another fragment destination from fragmentA (or B) and come back :

E/AndroidRuntime: FATAL EXCEPTION: main Process: app, PID: 30471 java.lang.IllegalStateException: Fragment no longer exists for key f#0: unique id 7ee10572-a2f6-4e46-8227-daaad57fd649 at androidx.fragment.app.FragmentManager.getFragment(FragmentManager.java:772) at androidx.viewpager2.adapter.FragmentStateAdapter.restoreState(FragmentStateAdapter.java:549) at androidx.viewpager2.widget.ViewPager2.restorePendingState(ViewPager2.java:350) at androidx.viewpager2.widget.ViewPager2.dispatchRestoreInstanceState(ViewPager2.java:375) at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:3864) at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:3864) at android.view.View.restoreHierarchyState(View.java:19793) at androidx.fragment.app.Fragment.restoreViewState(Fragment.java:573) at androidx.fragment.app.FragmentStateManager.restoreViewState(FragmentStateManager.java:356) at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1189) at androidx.fragment.app.FragmentManager.addAddedFragments(FragmentManager.java:2224) at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:1997) at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:1953) at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:1849) at androidx.fragment.app.FragmentManager$4.run(FragmentManager.java:413) at android.os.Handler.handleCallback(Handler.java:883) at android.os.Handler.dispatchMessage(Handler.java:100) at android.os.Looper.loop(Looper.java:214) at android.app.ActivityThread.main(ActivityThread.java:7356) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)

Can you help? Thanks

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:21
  • Comments:14

github_iconTop GitHub Comments

2reactions
alkochercommented, Sep 14, 2022

@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.

2reactions
alkochercommented, Dec 6, 2021

Just create adapter with Fragment: FragmentStateAdapter(fragment)

Don’t use FragmentStateAdapter(fragmentActivity)

Read more comments on GitHub >

github_iconTop Results From Across the Web

android - ViewPager2 inside a fragment leaks after replacing ...
Removing adapter from ViewPager2 in onDestroyView method of fragment solved the memory leak issue with FragmentStateAdapter
Read more >
Fix ViewPager2 's Memory Leak by Lifecycle-Aware Component
There is a common memory-leak issue of ViewPager2, for example, through a large or unknown number of fragments, use FragmentStateAdapter ...
Read more >
FragmentStateAdapter is causing a memory leak when used ...
Then FragmentStateAdapters internal mFragmentMaxLifecycleEnforcer is causing a memory leak because it's holding a hard reference to the ViewPager2 instance.
Read more >
Fix your Android Memory Leaks in Fragments - Procore
A memory leak occurs when an object's reference is held on to after its purpose has been served. As a result, this prevents...
Read more >
Can anyone help me with a FragmentStatePager memory leak ...
Can anyone help me with a FragmentStatePager memory leak problem? ... public class ViewPagerAdapter extends FragmentStatePagerAdapter ...
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