Daniele B
04/15/2024, 12:35 PM@Composable
fun MyComposable(url: String) {
val webClient = CustomWebViewClient()
val showLoadingMessage by remember { mutableStateOf(true) }
Box(modifier = Modifier.fillMaxSize()) {
AndroidView(
factory = { context -> WebView(context).apply { webViewClient = webClient } },
update = { webView -> if (!webClient.loaded) { webView.loadUrl(url) } else { showLoadingMessage = false } }
)
if (showLoadingMessage) {
Text(text = "I am loading the page...")
}
}
}
class CustomWebViewClient: WebViewClient(){
var loaded = false
override fun onPageFinished(view: WebView?, url: String?) {
loaded = true
}
}
Stylianos Gakis
04/15/2024, 12:47 PMonPageFinished
not called for you when the loading is done? If yes you can use that to know about this.
With that said, you seem to be creating a new WebViewClient without remembering it so on every recomposition.
Then inside there you have a loaded
value which will just be false
all the time more or less since on each recomposition you get the new instance, while you’ve passed only the first instance you had before to your WebView in the factory lambda which should only have run once.
Consider taking a look here https://github.com/google/accompanist/tree/main/web or even copy-pasting all the code from here to get a working version of a webview first and then go from there.Daniele B
04/15/2024, 1:04 PMonPageFinished
is called correctly.
I will have a look at Accompanist, hoping it's clear to understand.Daniele B
04/16/2024, 2:06 PM@Composable
fun MyComposable(url: String) {
val webClient : CustomWebViewClient = remember { CustomWebViewClient() }
Box(modifier = Modifier.fillMaxSize()) {
AndroidView(
factory = { context -> WebView(context).apply { webViewClient = webClient } },
update = { webView -> webView.loadUrl(url) }
)
if (!webClient.loaded) {
Text(text = "I am loading the page...")
}
}
}
class CustomWebViewClient: WebViewClient() {
@Stable var loaded = false
override fun onPageFinished(view: WebView?, url: String?) {
loaded = true
}
}
Stylianos Gakis
04/16/2024, 3:16 PMloaded
from?
Right now it’s just a var, there’s no way for compose to be able to observe it if it changes so that it gets the latest value out of it.
If it were a var loaded by mutableStateOf(false)
then you would probably be able to actually oberve its state. But with that said, I’d need to look at more of your code to understand what you want to happen hereDaniele B
04/16/2024, 3:35 PMvar loaded by mutableStateOf(false)
on the CustomWebViewClient classDaniele B
04/16/2024, 3:35 PM