1

I use Navigation Component from Jetpack in my app. I have 2 fragments, e.g. FirstFragment and SecondFragment and I have navigation in navigation graph from the first to second. Everything works correctly this way. I want to add child fragment to FirstFragment. So in onCreateView method of FirstFragment class I added the line

getChildFragmentManager().beginTransaction().add(R.id.fragment_container, new SimpleFragment()).commit();

This way I have an inner fragment in FirstFragment. Navigation to SecondFragment from FirstFragment still works correctly, but when I press the back button in SecondFragment I get this error

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.abc.def, PID: 28856
    java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
        at android.view.ViewGroup.addViewInner(ViewGroup.java:4937)
        at android.view.ViewGroup.addView(ViewGroup.java:4768)
        at android.view.ViewGroup.addView(ViewGroup.java:4708)
        at android.view.ViewGroup.addView(ViewGroup.java:4681)
        at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1353)
        at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1642)
        at androidx.fragment.app.FragmentManager.moveFragmentToExpectedState(FragmentManager.java:1736)
        at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1800)
        at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:3096)
        at androidx.fragment.app.FragmentManager.dispatchActivityCreated(FragmentManager.java:3050)
        at androidx.fragment.app.Fragment.performActivityCreated(Fragment.java:2688)
        at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1369)
        at androidx.fragment.app.FragmentManager.addAddedFragments(FragmentManager.java:2633)
        at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:2377)
        at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:2333)
        at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:2230)
        at androidx.fragment.app.FragmentManager$3.run(FragmentManager.java:414)
        at android.os.Handler.handleCallback(Handler.java:790)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6494)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

I get this error if I either press the back button or call navController.navigateUp() or navController.popBackStack(). If I remove the line

getChildFragmentManager().beginTransaction().add(R.id.fragment_container, new SimpleFragment()).commit();

from FirstFragment I have no error, so this means that there is a problem with child fragment manager, right? What is wrong here?

cactus4
  • 128
  • 9
Kiryl Tkach
  • 3,118
  • 5
  • 20
  • 36
  • Is `R.id.fragment_container` a container in your `FirstFragment`? Or in your activity's layout? – ianhanniballake Sep 21 '19 at 22:06
  • @ianhanniballake it is in FirstFragment – Kiryl Tkach Sep 21 '19 at 22:56
  • Possible duplicate of [The specified child already has a parent. You must call removeView() on the child's parent first (Android)](https://stackoverflow.com/questions/28071349/the-specified-child-already-has-a-parent-you-must-call-removeview-on-the-chil) – Martin Zeitler Sep 22 '19 at 04:26
  • The problem is that you attempt inflating that child-fragment twice. This either has to be prevented or it has to be removed previously. Or you could change method `.add()` to `.replace()`. It should be obvious, when it only works the first, but not the second time. – Martin Zeitler Sep 22 '19 at 04:40

2 Answers2

0

If I understand this correctly without knowing what you have without looking at your layout of "fragment_container" tells me that its type of ViewGroup which can have only one child at this point if you don't provide more information to it.

What that means is fragment_container already has a child and which you need to remove before you add your SimpleFragment.

Lets assume you have your first layout of fragment something like this

<someView>

  <fragment_container/>  // hear you are loading your first fragment and trying load the SimpleFragment as well which is causing problem.

</someView>

what you need is

<someView>

  <fragment_container/>  only load First Fragment "first_fragment_layout.xml"

</someView>

And in layout of First fragment

first_fragment_layout.xml

<someView>

  <viewBlasBla/>

  <viewBlasBla/>

  <fragment_container/>  load SimpleFragment here

</someView>

as you can see, you are indeed trying to do nested fragment which is not a Good Practice. try to avoid this if possible :).

you can reach me here

Pankaj Nimgade
  • 4,529
  • 3
  • 20
  • 30
0

The problem was in SimpleFragment. I was keeping reference to the root view of that fragment (had View mRootView field), and didn't recreate it in case it wasn't null.

Kiryl Tkach
  • 3,118
  • 5
  • 20
  • 36