diff --git a/cmplibrary/src/main/java/com/sourcepoint/cmplibrary/SpConsentLibMobileCore.kt b/cmplibrary/src/main/java/com/sourcepoint/cmplibrary/SpConsentLibMobileCore.kt index 03cd2682b..bb7033bb7 100644 --- a/cmplibrary/src/main/java/com/sourcepoint/cmplibrary/SpConsentLibMobileCore.kt +++ b/cmplibrary/src/main/java/com/sourcepoint/cmplibrary/SpConsentLibMobileCore.kt @@ -86,7 +86,7 @@ class SpConsentLibMobileCore( } // used to store accessibility state of main view's children - private var a11ySnapshot: WeakHashMap = WeakHashMap() + private var a11ySnapshot: WeakHashMap = WeakHashMap() private var messageUI: SPMessageUI? = null fun getOrCreateMessageUI(): SPMessageUI { @@ -139,7 +139,7 @@ class SpConsentLibMobileCore( private fun renderNextMessageIfAny() = if (pendingActions == 0 && messagesToDisplay.isEmpty()) { - messageUI = null + disposeMessageUI() spClient.onSpFinished(spConsents) } else if (messagesToDisplay.isNotEmpty()) { val messageToRender = messagesToDisplay.removeFirst() @@ -311,11 +311,17 @@ class SpConsentLibMobileCore( private fun restoreChildrenA11y() { for ((view, accessibilityState) in a11ySnapshot.entries.toList()) { - view.importantForAccessibility = accessibilityState + view?.importantForAccessibility = accessibilityState } a11ySnapshot.clear() } + private fun disposeMessageUI() { + // Remove view from parent before nullifying to avoid NPE in ViewGroup + messageUI?.let { removeView(it as View) } + messageUI = null + } + override fun showView(view: View) { mainView?.let { parentView -> view.layoutParams = ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT) @@ -382,7 +388,7 @@ class SpConsentLibMobileCore( spClient.onConsentReady(SPConsents(userData)) pendingActions-- if (pendingActions == 0) { - messageUI = null + disposeMessageUI() spClient.onSpFinished(spConsents) } }.onFailure { @@ -396,7 +402,7 @@ class SpConsentLibMobileCore( finished(view) } ActionType.PM_DISMISS -> { - if (getOrCreateMessageUI().isFirstLayer) { + if (messageUI?.isFirstLayer == true) { pendingActions-- finished(view) } @@ -435,7 +441,7 @@ class SpConsentLibMobileCore( } override fun finished(view: View) { - getOrCreateMessageUI().isPresenting = false + messageUI?.isPresenting = false runOnMain { spClient.onUIFinished(view) } renderNextMessageIfAny() }