From b1467cb18ad19ce71691b99cc6a3252d2951daba Mon Sep 17 00:00:00 2001 From: michael guerra Date: Wed, 21 Oct 2026 11:11:26 -0400 Subject: [PATCH] DIA-6317 fix showView/removeView crashes on reused message view - disposeMessageUI: post removeView to the main thread, since it can be called from a background coroutine (CalledFromWrongThreadException) - showView: never add a view that already has a parent, so a duplicate onUIReady for a reused web view doesn't crash (IllegalStateException) Co-Authored-By: Claude Opus 5.5 (1M context) --- .../cmplibrary/SpConsentLibMobileCore.kt | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/cmplibrary/src/main/java/com/sourcepoint/cmplibrary/SpConsentLibMobileCore.kt b/cmplibrary/src/main/java/com/sourcepoint/cmplibrary/SpConsentLibMobileCore.kt index bb7033bb7..541a0a92a 100644 --- a/cmplibrary/src/main/java/com/sourcepoint/cmplibrary/SpConsentLibMobileCore.kt +++ b/cmplibrary/src/main/java/com/sourcepoint/cmplibrary/SpConsentLibMobileCore.kt @@ -317,18 +317,25 @@ class SpConsentLibMobileCore( } private fun disposeMessageUI() { - // Remove view from parent before nullifying to avoid NPE in ViewGroup - messageUI?.let { removeView(it as View) } + // Remove view from parent before nullifying to avoid NPE in ViewGroup. + // This can be called from a background coroutine, so the view removal must happen on main. + val ui = messageUI as? View messageUI = null + ui?.let { runOnMain { removeView(it) } } } override fun showView(view: View) { mainView?.let { parentView -> + // the same message view may be "shown" more than once (e.g. onUIReady called again for a + // re-used web view), so make sure we never add a view that already has a parent + (view.parent as? ViewGroup)?.takeIf { it != parentView }?.removeView(view) view.layoutParams = ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT) - if (hideAppsViewsFromAccessibilityWhileMessageIsDisplayed) { - hideMainViewChildrenFromAccessibility(parentView) + if (view.parent == null) { + if (hideAppsViewsFromAccessibilityWhileMessageIsDisplayed) { + hideMainViewChildrenFromAccessibility(parentView) + } + parentView.addView(view) } - parentView.addView(view) view.bringToFront() view.requestLayout() view.requestFocus()