Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add note about Android control parenting when control already has a parent #1923

Open
Eilon opened this issue Nov 29, 2023 · 3 comments
Open
Labels
doc-enhancement Improve the current content [org] Pri3

Comments

@Eilon
Copy link
Member

Eilon commented Nov 29, 2023

This is motivated by this issue: dotnet/maui#15920

On Android you can't 'move' a control from one parent to another without first removing it from the first parent. This is different from other platforms where this is allowed.

The exception you get is Java.Lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

I suggest updating the docs with the following:

  1. Note this different in platforms where we talk about adding controls programatically
  2. Include the exception text so that we get better SEO
  3. Specify how to do it correctly in MAUI with the C# APIs (it's not Java, so you don't call removeView())

cc @hartez

@davidbritch davidbritch self-assigned this Nov 30, 2023
@davidbritch davidbritch added doc-enhancement Improve the current content [org] 🗺️ reQUEST Triggers an issue to be imported into Quest. labels Nov 30, 2023
@mattleibow
Copy link
Member

This also happens on Windows.

Might be worth stating that you should always do it...

@github-actions github-actions bot added 📌 seQUESTered Identifies that an issue has been imported into Quest. and removed 🗺️ reQUEST Triggers an issue to be imported into Quest. labels Dec 1, 2023
@davidbritch davidbritch removed the 📌 seQUESTered Identifies that an issue has been imported into Quest. label Dec 22, 2023
@davidbritch davidbritch removed the Pri3 label Jan 2, 2024
@LBoullosa
Copy link

I am having this issue when switching between two control templates in a TemplatedView. In my case, the parent is always the same, just based on a condition I show one control template or another one. So, i am a bit confused about the exception.

@davidbritch davidbritch removed their assignment Feb 28, 2024
@JulioVillanueva
Copy link

JulioVillanueva commented Mar 14, 2024

UPDATE: 26042024 please ignore this comment, I updated to maui 8.0.21 and now Is working well in windows and android, by not using the work around of setting the parent to null i also fixed a memory leak i had been hunting down

Important note

Why is this a dulicate of issue 15920 and 20310? why are they close? Have they been solved in a PR or merge that I missed? why are they only tagged as android?

Context

I have hit this issue for weeks in my windows App (tested on win10 and android with and without debugging) and as you can see in the screenshots provided in some comments in 15920 the error raised in windows plataform "No installed componets were detected" was useless and the debugger was often crashing before even raising an error so I could not find any help or share my work around. Finally I decided to try other plataforms and it was in android that I found an error "Java.Lang.IllegalStateException: The specified child already has a parent" that lead to 15920 and 20310.

Issue

My case was basically a grid of cells set as content of a control, the grid was made with a verticalStackLayout full of HorizontalStackLayouts full of my cuscomCellControls (going forwards cell). And it was crashing only if I reused the cells instead of creating a new instance. It is quite similar to 15920 and 20310 which both have in the issue itself and in some comments code to test it.

Workaround:

In my case setting the handler to null fixed the issue. In fact I recommend using the extension method CheckHandlers since its current implementation is setting the handler to null.

cell.Handler = null;

The method CheckHandlers with his current implementation seems a better option because it's covering more scenarios

image

workarounds that are not working for me

Neither setting the parent to null or getting the parent to remove the child seems to be solving the issue but they also dont affecting my workaround.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
doc-enhancement Improve the current content [org] Pri3
Projects
None yet
Development

No branches or pull requests

5 participants