一、概述
双层Fragment+ViewPager2嵌套来回切换导致如下异常
ViewPager2 FragmentManager is already executing transactions
二、解决办法
FragmentStateAdapter的默认构造函数使用的是:
public FragmentStateAdapter(@NonNull FragmentActivity fragmentActivity) { this(fragmentActivity.getSupportFragmentManager(), fragmentActivity.getLifecycle()); }
改成下面的:
calendarAdapter = CalendarEventAdapter(childFragmentManager,requireActivity().lifecycle)
class CalendarEventAdapter(fragmentManager: FragmentManager, lifecycle: Lifecycle) : FragmentStateAdapter(fragmentManager, lifecycle) { private var fragments = ArrayList<Fragment>() override fun getItemCount(): Int { return fragments.size } override fun createFragment(position: Int): Fragment { return fragments[position] } fun addFragment(fragment: Fragment) { fragments.apply { fragments.add(fragment) } } }
标签:already,Fragment,executing,ViewPager2,FragmentManager,fragments From: https://www.cnblogs.com/tony-yang-flutter/p/18103987