Home > Mobile >  How to use fragment into another fragment in Android
How to use fragment into another fragment in Android

Time:01-08

In my application I have fragment and I want to use this fragment into another fragment!
I write below codes, but when open fragment shows me force close error.
My code:

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        parentFragmentManager.beginTransaction().add(
            R.id.calendarContainer, calendar, PersianCaldroidFragment::class.java.name
        ).commit()
}

Error message:

 java.lang.IllegalStateException: FragmentManager is already executing transactions
        at androidx.fragment.app.FragmentManager.ensureExecReady(FragmentManager.java:1686)
        at androidx.fragment.app.FragmentManager.execSingleAction(FragmentManager.java:1716)
        at androidx.fragment.app.BackStackRecord.commitNow(BackStackRecord.java:317)
        at androidx.viewpager2.adapter.FragmentStateAdapter$FragmentMaxLifecycleEnforcer.updateFragmentMaxLifecycle(FragmentStateAdapter.java:726)
        at androidx.viewpager2.adapter.FragmentStateAdapter$FragmentMaxLifecycleEnforcer$3.onStateChanged(FragmentStateAdapter.java:657)
        at androidx.lifecycle.LifecycleRegistry$ObserverWithState.dispatchEvent(LifecycleRegistry.java:360)
        at androidx.lifecycle.LifecycleRegistry.forwardPass(LifecycleRegistry.java:271)
        at androidx.lifecycle.LifecycleRegistry.sync(LifecycleRegistry.java:313)
        at androidx.lifecycle.LifecycleRegistry.moveToState(LifecycleRegistry.java:151)
        at androidx.lifecycle.LifecycleRegistry.handleLifecycleEvent(LifecycleRegistry.java:134)
        at androidx.fragment.app.Fragment.performStart(Fragment.java:3167)
        at androidx.fragment.app.FragmentStateManager.start(FragmentStateManager.java:588)
        at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:279)
        at androidx.fragment.app.FragmentStore.moveToExpectedState(FragmentStore.java:113)
        at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1424)
        at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:2968)
        at androidx.fragment.app.FragmentManager.dispatchStart(FragmentManager.java:2893)
        at androidx.fragment.app.Fragment.performStart(Fragment.java:3171)
        at androidx.fragment.app.FragmentStateManager.start(FragmentStateManager.java:588)
        at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:279)
        at androidx.fragment.app.FragmentStore.moveToExpectedState(FragmentStore.java:113)
        at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1424)
        at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:2968)
        at androidx.fragment.app.FragmentManager.dispatchStart(FragmentManager.java:2893)
        at androidx.fragment.app.FragmentController.dispatchStart(FragmentController.java:274)
        at androidx.fragment.app.FragmentActivity.onStart(FragmentActivity.java:359)

How can I fix it?

CodePudding user response:

try replacing parentFragmentManager with childFragmentManager

  • Related