Thread
#compose
    m

    MaxUt

    1 year ago
    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 ?
    @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 :
    val parent = WebViewSingleton.webViews[type]!!.parent as ViewGroup
    WebViewSingleton.webViews[type]!!.removeView(parent)
    AndroidView({
         WebViewSingleton.webViews[type]!!
    })