From e3bea87ee3ac806bae112806894b6c9740d1750e Mon Sep 17 00:00:00 2001 From: Bartlomiej Moskal Date: Tue, 1 Oct 2024 10:44:03 +0200 Subject: [PATCH] Android: When setting new parent for childView remove old one A view cannot have two parents. If the view already has a parent, you must first call removeView() on the child's parent. Fixes: QTBUG-129524 Pick-to: 6.7 Change-Id: I6a8340ed8db58a77626be17366d5c71bdfc8c762 Reviewed-by: Assam Boudjelthia (cherry picked from commit ecd4623f05abf419db6f0c3bdad406c1883b6509) Reviewed-by: Qt Cherry-pick Bot --- .../jar/src/org/qtproject/qt/android/QtLayout.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/android/jar/src/org/qtproject/qt/android/QtLayout.java b/src/android/jar/src/org/qtproject/qt/android/QtLayout.java index 95c62d58c40..ac886b5fc8c 100644 --- a/src/android/jar/src/org/qtproject/qt/android/QtLayout.java +++ b/src/android/jar/src/org/qtproject/qt/android/QtLayout.java @@ -13,6 +13,7 @@ import android.view.Display; import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; +import android.view.ViewParent; class QtLayout extends ViewGroup { @@ -201,14 +202,19 @@ class QtLayout extends ViewGroup { if (!checkLayoutParams(params)) return; + final ViewParent parent = childView.getParent(); + // View is already in the layout and can therefore be updated - final boolean canUpdate = (this == childView.getParent()); + final boolean canUpdate = (this == parent); if (canUpdate) { childView.setLayoutParams(params); if (forceRedraw) invalidate(); } else { + // If the parent was already set it need to be removed first + if (parent != null && parent instanceof ViewGroup) + ((ViewGroup)parent).removeView(childView); addView(childView, params); } }