Issue
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?
Solution
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.
Answered By - Kiryl Tkach
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.