Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
304 views
in Technique[技术] by (71.8m points)

Embedding a Xamarin.Forms fragment in a fragment causing "child already has a parent" crash

I'm working on a library that would allow you to use Xamarin.Forms Pages with the native navigation service provided by MvvmCross.

The Android implementation has an issue when using Fragments. If you look at the library / sample project a crash occurs when you press the back button after loading the second fragment.

Steps to Reproduce:

  1. Launch App
  2. Press "Fragment Navigation Stack"
  3. Press "Next"
  4. Press the back button.

I've been pulling my hair out trying to figure out how to stop this error from occurring.

The app crashes with this error.

Java.Lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
  --- End of managed Java.Lang.IllegalStateException stack trace ---
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:5106)
    at android.view.ViewGroup.addView(ViewGroup.java:4935)
    at android.view.ViewGroup.addView(ViewGroup.java:4875)
    at android.view.ViewGroup.addView(ViewGroup.java:4848)
    at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:326)
    at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1199)
    at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1368)
    at androidx.fragment.app.FragmentManager.moveFragmentToExpectedState(FragmentManager.java:1446)
    at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1509)
    at androidx.fragment.app.BackStackRecord.executeOps(BackStackRecord.java:447)
    at androidx.fragment.app.FragmentManager.executeOps(FragmentManager.java:2181)
    at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:2004)
    at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:1959)
    at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:1861)
    at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:2641)
    at androidx.fragment.app.FragmentManager.dispatchActivityCreated(FragmentManager.java:2589)
    at androidx.fragment.app.Fragment.performActivityCreated(Fragment.java:2723)
    at androidx.fragment.app.FragmentStateManager.activityCreated(FragmentStateManager.java:346)
    at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1200)
    at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1368)
    at androidx.fragment.app.FragmentManager.moveFragmentToExpectedState(FragmentManager.java:1446)
    at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1509)
    at androidx.fragment.app.BackStackRecord.executePopOps(BackStackRecord.java:510)
    at androidx.fragment.app.FragmentManager.executeOps(FragmentManager.java:2178)
    at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:2004)
    at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:1959)
    at androidx.fragment.app.FragmentManager.popBackStackImmediate(FragmentManager.java:670)
    at androidx.fragment.app.FragmentManager.popBackStackImmediate(FragmentManager.java:581)

    at androidx.fragment.app.FragmentManager.handleOnBackPressed(FragmentManager.java:553)
    at androidx.fragment.app.FragmentManager$1.handleOnBackPressed(FragmentManager.java:350)
    at androidx.activity.OnBackPressedDispatcher.onBackPressed(OnBackPressedDispatcher.java:192)
    at androidx.activity.ComponentActivity.onBackPressed(ComponentActivity.java:314)
    at android.app.Activity.onKeyUp(Activity.java:3641)
    at android.view.KeyEvent.dispatch(KeyEvent.java:2825)
    at android.app.Activity.dispatchKeyEvent(Activity.java:3958)
    at androidx.core.app.ComponentActivity.superDispatchKeyEvent(ComponentActivity.java:122)
    at androidx.core.view.KeyEventDispatcher.dispatchKeyEvent(KeyEventDispatcher.java:84)
    at androidx.core.app.ComponentActivity.dispatchKeyEvent(ComponentActivity.java:140)
    at androidx.appcompat.app.AppCompatActivity.dispatchKeyEvent(AppCompatActivity.java:569)
    at androidx.appcompat.view.WindowCallbackWrapper.dispatchKeyEvent(WindowCallbackWrapper.java:59)
    at androidx.appcompat.app.AppCompatDelegateImpl$AppCompatWindowCallback.dispatchKeyEvent(AppCompatDelegateImpl.java:3054)
    at com.android.internal.policy.DecorView.dispatchKeyEvent(DecorView.java:367)
    at android.view.ViewRootImpl$ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:5413)
    at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:5281)
    at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4788)
    at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4841)
    at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4807)
    at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4947)
    at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4815)
    at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:5004)
    at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4788)
    at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4841)
    at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4807)
    at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4815)
    at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4788)
    at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4841)
    at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4807)
    at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4980)
    at android.view.ViewRootImpl$ImeInputStage.onFinishedInputEvent(ViewRootImpl.java:5141)
    at android.view.inputmethod.InputMethodManager$PendingEvent.run(InputMethodManager.java:3064)
    at android.view.inputmethod.InputMethodManager.invokeFinishedInputEventCallback(InputMethodManager.java:2607)
    at android.view.inputmethod.InputMethodManager.finishedInputEvent(InputMethodManager.java:2598)
    at android.view.inputmethod.InputMethodManager$ImeInputEventSender.onInputEventFinished(InputMethodManager.java:3041)
    at android.view.InputEventSender.dispatchInputEventFinished(InputEventSender.java:143)
    at android.os.MessageQueue.nativePollOnce(Native Method)
    at android.os.MessageQueue.next(MessageQueue.java:336)
    at android.os.Looper.loop(Looper.java:174)
    at android.app.ActivityThread.main(ActivityThread.java:7356)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)

question from:https://stackoverflow.com/questions/65660589/embedding-a-xamarin-forms-fragment-in-a-fragment-causing-child-already-has-a-pa

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...