导航架构组件:FragmentManager 已经在执行事务

Navigation Architecture Component: FragmentManager is already executing transactions

我有包含 3 个主要片段的 BottomNavigationView:A、B 和 C。当我尝试使用 BottomNavigationView 以任何顺序再次打开 FragmentC 时,我看到一个错误:FragmentManager 已经在执行交易

BUT 没有 setupViewPager() 行一切正常,我不止一次打开 FragmentC。否则,将显示错误。

我不明白发生了什么。拜托,我真的需要你的帮助。

setViews() 它是在 BaseFragment 中定义并在 onViewCreated()

中调用的函数

片段C

class FragmentC: BaseFragment() {

    override val menuResId: Nothing? = null
    override val contentResId = R.layout.fragment_c
    override val baseToolbar = R.id.toolbar

    override fun setViews(view: View) {
        setupViewPager()   <----- error
    }

    private fun setupViewPager() {
        val viewPagerAdapter = ViewPagerAdapter(baseActivity.supportFragmentManager).apply {
            addFragment(FragmentE(), "E")
            addFragment(FragmentF(), "F")
        }
        viewPager.adapter = viewPagerAdapter     <----- error
        tabLayout.setupWithViewPager(viewPager)
    }
}

ViewPagerAdapter

class ViewPagerAdapter(fm: FragmentManager) : FragmentStatePagerAdapter(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {

    private val titleList : MutableList<String> = ArrayList()
    private val fragmentList : MutableList<Fragment> = ArrayList()

    override fun getItem(position: Int): Fragment {
        return fragmentList[position]
    }

    override fun getCount(): Int {
        return fragmentList.size
    }

    fun addFragment(fragment: Fragment, title: String){
        titleList.add(title)
        fragmentList.add(fragment)
    }

    override fun getPageTitle(position: Int): CharSequence? {
        return titleList[position]
    }
}

错误

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.project, PID: 20461
    java.lang.IllegalStateException: FragmentManager is already executing transactions
        at androidx.fragment.app.FragmentManager.ensureExecReady(FragmentManager.java:1778)
        at androidx.fragment.app.FragmentManager.execSingleAction(FragmentManager.java:1814)
        at androidx.fragment.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:303)
        at androidx.fragment.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:270)
        at androidx.viewpager.widget.ViewPager.setAdapter(ViewPager.java:513)
        at com.example.project.views.communication.CommunicationFragment.setupViewPager(CommunicationFragment.kt:26)
        at com.example.project.views.communication.CommunicationFragment.setViews(CommunicationFragment.kt:18)
        at com.example.project.views.base.BaseFragment.onViewCreated(BaseFragment.kt:37)
        at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:332)
        at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1187)
        at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1356)
        at androidx.fragment.app.FragmentManager.moveFragmentToExpectedState(FragmentManager.java:1434)
        at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1497)
        at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:2625)
        at androidx.fragment.app.FragmentManager.dispatchActivityCreated(FragmentManager.java:2577)
        at androidx.fragment.app.Fragment.performActivityCreated(Fragment.java:2722)
        at androidx.fragment.app.FragmentStateManager.activityCreated(FragmentStateManager.java:346)
        at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1188)
        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.run(FragmentManager.java:413)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

也许有人知道为什么会出现错误?

使用 childFragmentManager 而不是 supportFragmentManager 然后,要么像这样覆盖方法 saveState

override fun saveState(): Parcelable {
    return null
}

或将 FragmentStatePagerAdapter 更改为 FragmentPagerAdapter