https://kotlinlang.org logo
#compose
Title
# compose
m

MaxUt

06/21/2021, 3:38 PM
Hi guys, I'm storing Webview objects in a Singleton in order to keep their state. When my Composable gets recomposed it simply pass the webview objects to the AndroidView factory. This works perfectly when I change of Composable, however when the recomposition is triggered by an other event such as a Composable passing in front of the webview Composable, I get the following error :
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
Any ideas ?
Copy code
@Composable
fun AppWebView(
    urlToRender: String,
    type: WebViewType,
    onWebViewBackStateChange: (Boolean) -> Unit,
) {
    val client = object : WebViewClient() {
        override fun doUpdateVisitedHistory(view: WebView?, url: String?, isReload: Boolean) {
            onWebViewBackStateChange(view!!.canGoBack())
            super.doUpdateVisitedHistory(view, url, isReload)
        }
    }
    if (WebViewSingleton.webViews[type] === null) {
        AndroidView({ context ->
            WebView(context).apply{
                webViewClient = client
            }.also { WebViewSingleton.webViews[type] = it }
        })
        DisposableEffect(WebViewSingleton.webViews[type], urlToRender) {
            WebViewSingleton.webViews[type]?.loadUrl(urlToRender)
            onDispose {
                WebViewSingleton.webViews[type]?.stopLoading()
            }
        }
    } else {
        // When recomposing, we enter here and the app crashes
        AndroidView({
            WebViewSingleton.webViews[type]!!
        })
    }
}
I tried removing the webview parent before adding it to the AndroidView again but it doesn't work :
Copy code
val parent = WebViewSingleton.webViews[type]!!.parent as ViewGroup
WebViewSingleton.webViews[type]!!.removeView(parent)
AndroidView({
     WebViewSingleton.webViews[type]!!
})
7 Views