Hey it is possible to restore WebView state in Com...
# compose
m
Hey it is possible to restore WebView state in Compose? Thread 🧵
Copy code
@Composable
fun WebsiteScreen(
    state: WebsiteState,
) {
 
    AndroidView({
        WebView(it).apply {
            id = R.id.webViewId
            with(settings) {
                javaScriptEnabled = true
                javaScriptCanOpenWindowsAutomatically = true
                setSupportMultipleWindows(true)
            }

            webViewClient = AppWebViewClient(
                trustUntrustedCert = state.canTrustUntrustedCert,
                onPageVisible = state.onPageVisible,
                onUrlLoad = state.onUrlClick
            )
            webChromeClient = AppChromeWebViewClient(state.onTargetBlankUrl)
        }
    }) {
        if (state.postBody != null) {
            it.postUrl(state.url, state.postBody.toByteArray())
        } else {
            it.setUrl(state.url)
        }
    }
}
When I'm going to another screen and back, screen recompose and I lose a session in WebView.
When I use android.view.EditText and setId. After I return to the WebsiteScreen I don't lose a state. One workaround is to provide Webview by CompositeLocal. And add if statement in update block, but it seems like hax 🙂
p
I am also facing same issue , what is WebsiteState here in your code ??
u
@Vaibhav Jaiswal