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

Mateusz Apacki

12/06/2021, 2:20 PM
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

Pratibha sharma

02/07/2024, 4:53 AM
I am also facing same issue , what is WebsiteState here in your code ??
5 Views