Android crash when adding a View to a Layout that is already in another Layout #15920
Labels
area-layout
StackLayout, GridLayout, ContentView, AbsoluteLayout, FlexLayout, ContentPresenter
platform/android 🤖
s/triaged
Issue has been reviewed
s/verified
Verified / Reproducible Issue ready for Engineering Triage
t/bug
Something isn't working
Milestone
Description
Maui crashes on Android when you create a View (such as a button), assign it to a Layout, assign that layout to the Content property and then reuse that same control in a 2nd layout. When you assign the 2nd layout to Content property it causes a crash.
The crash results in the error:
Java.Lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
This is not reproducible on iOS but consistently happening on Android (tested on Android 13 but not other versions). Ideally Maui would detach any Views from its parent before adding a View to a different Layout.
Another point is the layout must be assigned to the Content property for this error to happen. For example, if you assign the same control to 2 layouts but only assign 1 layout to Content that is okay. But when you assign the 2nd layout to the Content property it breaks.
Steps to Reproduce
Result: App Crashes with error "Java.Lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first."
Expected: To be able to reassign a button to different layouts without having to manually remove it from previous layouts.
Link to public reproduction project repository
https://github.com/gerhartz/maui_android_crash
Version with bug
7.0.86
Last version that worked well
Unknown/Other
Affected platforms
Android
Affected platform versions
Broken on Android 13, possibly earlier versions
Did you find any workaround?
((Layout)view.Parent)?.Remove(view);
Avoid reassigning a control to a different layout after it has already been added to avoid hitting the IllegalStateException.
Duplicating the control and adding the duplicate to a layout instead of reusing the original can be a workaround, although in some cases this is not feasible to maintain multiple duplicate controls.
Relevant log output
The text was updated successfully, but these errors were encountered: