Leak Canary detects memory leaks for TabLayout with ViewPager2

后端 未结 4 1340
遇见更好的自我
遇见更好的自我 2020-12-18 00:35

I followed the official documentation to setup a TabLayout with ViewPager2. I used the TabLayoutMediator this way to connect the TabLayout with the ViewPager2:



        
4条回答
  •  孤城傲影
    2020-12-18 00:53

    I think this is a bug, its listed on google.

    https://issuetracker.google.com/issues/151212195 https://issuetracker.google.com/issues/154751401

    There is a solution mentioned in the comments that seems to be working for me. Make your FragmentStateAdapter user the constructor

     public FragmentStateAdapter(@NonNull FragmentManager fragmentManager,
                @NonNull Lifecycle lifecycle) {
    

    From your container fragment create the adapter as follows:

    FragmentManager fm = getChildFragmentManager();
    Lifecycle lifecycle = getViewLifecycleOwner().getLifecycle();
    fragmentAdapter = new FragmentAdapter(fm, lifecycle);
    

提交回复
热议问题