MaxUt
04/30/2021, 9:34 AMFilip Wiesner
04/30/2021, 9:37 AMMaxUt
04/30/2021, 9:40 AM@Composable
fun ParentComposable() {
var webView by rememberSaveable { mutableStateOf<WebView?>(null) }
var webViewClient by rememberSaveable { mutableStateOf<WebViewClient?>(null) }
Scaffold(
...
) {
ChildComposable(
urlToRender = AppConfig.url,
webView = webView,
onWebViewInit = onWebViewInit = { webView = it },
webViewClient = webViewClient,
onWebViewClientInit = { webViewClient = it },
)
}
}
@Composable
fun ChildComposable(urlToRender: String, webView: WebView?, onWebViewInit: (WebView) -> Unit, webViewClient: WebViewClient?, onWebViewClientInit: (WebViewClient) -> Unit) {
val client = object : WebViewClient() {
override fun onPageFinished(view: WebView, url: String) {
loadJs()
}
}
AndroidView({ context ->
WebView(context).apply{
layoutParams = ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT
)
settings.domStorageEnabled = true
settings.javaScriptEnabled = true
}.also { onWebViewInit(it) }
})
DisposableEffect(webView, urlToRender) {
onWebViewClientInit(client)
webView?.webViewClient = webViewClient!!
webView?.loadUrl(urlToRender)
onDispose {
webView?.stopLoading()
}
}
}
julioromano
04/30/2021, 10:13 AMMaxUt
04/30/2021, 10:40 AMif (TestSingleton.webView === null) {
AndroidView({ context ->
WebView(context).apply{
layoutParams = ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT
)
settings.domStorageEnabled = true
settings.javaScriptEnabled = true
webViewClient = client
}.also { TestSingleton.webView = it }
})
}
DisposableEffect(TestSingleton.webView, urlToRender) {
TestSingleton.webView?.loadUrl(urlToRender)
onDispose {
TestSingleton.webView?.stopLoading()
}
}
julioromano
04/30/2021, 11:04 AM(TestSingleton.webView != null)
? In that case when the AndroidView is created you should pass in the WebView instance previously saved in the singleton.MaxUt
04/30/2021, 11:49 AMZach Klippenstein (he/him) [MOD]
11/30/2021, 5:28 PM