compose-webview-multiplatform icon indicating copy to clipboard operation
compose-webview-multiplatform copied to clipboard

Android WebView is not disposed when Composable is disposed

Open Rumpel1984 opened this issue 1 year ago • 8 comments

This issue can only be ovserved in Android.

When you have multiple screens with WebView or navigate back to a composable with a WebView, the zoom is not working anymore. It looks like the Android WebView is not properly disposed of when the multiplatform WebView is disposed and therefore not reinitited when opening the second multiplatform WebView. At lease the Android WebView factory is only called when the first multiplatform WebView is opened, but not when navigating to any other WebView.

It might be related to that compose-navigation 2.8.0-alpha* version is not supported yet, but I can't confirm that.

I created a test app: https://github.com/Rumpel1984/ComposeWebViewMultiplatformZoomTest CMP version - 1.6.11 navigation-compose - 2.8.0-alpha09 kotlin - 2.0.20

The zoom is working properly the first time you open screen with a WebView. But as soon as you open another one, or return to the same WebView, it doesn't work anymore.

Rumpel1984 avatar Sep 03 '24 16:09 Rumpel1984

Please try out the latest release, version 1.9.40-alpha01.

KevinnZou avatar Sep 11 '24 14:09 KevinnZou

Unfortunately this is not fixing the issue. It still only works when the WebView is called the first time.

But I could confirm the issue is only in Andorid. iOS zoom works when switching screens and returning.

Rumpel1984 avatar Sep 13 '24 11:09 Rumpel1984

I updated to version 2.0.0 and still ahve the same issue. The Zoom is broken in Android when I leave the screen and return to it.

Rumpel1984 avatar Jun 07 '25 17:06 Rumpel1984

I tested the compose WebView for android (0.33.6) with jetbrains navigation (2.9.0-beta02) and Navigation 3 (1.0.0-alpha03). Both worked without issues:

  1. go to WebView Screen and zoom: works
  2. return to Home Screen
  3. go to WebView Screen and zoom: works

But when I use the WebView for multiplatform (2.0.0) with jetbrains navigation (2.9.0-beta02), it doesn't work.

  1. go to WebView Screen and zoom: works
  2. return to Home Screen
  3. go to WebView Screen and zoom: zoom is not working

Rumpel1984 avatar Jun 11 '25 14:06 Rumpel1984

@KevinnZou I'm experiencing this exact issue with version 2.0.0 of the library. The Zoom functionality works perfectly on the first load, but fails on subsequent navigations to the WebView screen. Environment Details:

compose-webview-multiplatform: 2.0.1 Compose Multiplatform: 1.8.2 Navigation Compose: 2.8.0-alpha13 Kotlin: 2.2.0 Target Android SDK: 36

hellosagar avatar Jul 26 '25 20:07 hellosagar

@KevinnZou code snippet


// Configure WebView settings for proper A4 scaling
    DisposableEffect(navigationKey) {
        println("XCXCX PreviewScreenRoute: DisposableEffect started with key = $navigationKey")
        
        webViewState.webSettings.apply {
            println("XCXCX PreviewScreenRoute: Applying WebView settings")
            // Enable JavaScript for zoom functionality
            isJavaScriptEnabled = true

            // Set zoom level for better A4 viewing (0.75 = 75%)
            zoomLevel = 0.75

            // Enable file access for local resources
            allowFileAccessFromFileURLs = true
            allowUniversalAccessFromFileURLs = true

            // Android-specific settings using only available properties
            androidWebSettings.apply {
                println("XCXCX PreviewScreenRoute: Applying Android settings")
                supportZoom = true
                useWideViewPort = true
                allowFileAccess = true
                domStorageEnabled = true
                textZoom = 100 // Keep text at 100% to prevent scaling issues
                defaultFontSize = 14 // Set a reasonable default font size
                loadsImagesAutomatically = true
                isAlgorithmicDarkeningAllowed = false // Disable for consistent appearance
                safeBrowsingEnabled = true
                println("XCXCX PreviewScreenRoute: Android settings applied - supportZoom = $supportZoom")
            }
        }

        onDispose { 
            println("XCXCX PreviewScreenRoute: DisposableEffect disposed with key = $navigationKey")
        }
    }
WebView(
                    state = webViewState,
                    modifier = Modifier.fillMaxSize(),
                    captureBackPresses = true,
                    onCreated = { webView ->
                        println("XCXCX PreviewScreenRoute: WebView onCreated callback")
                        // Apply settings again in onCreated to ensure they're set
                        webViewState.webSettings.apply {
                            println("XCXCX PreviewScreenRoute: Re-applying settings in onCreated")
                            isJavaScriptEnabled = true
                            zoomLevel = 0.75
                            allowFileAccessFromFileURLs = true
                            allowUniversalAccessFromFileURLs = true
                            
                            androidWebSettings.apply {
                                supportZoom = true
                                useWideViewPort = true
                                println("XCXCX PreviewScreenRoute: onCreated - supportZoom = $supportZoom")
                            }
                        }
                    },
                    onDispose = { webView ->
                        println("XCXCX PreviewScreenRoute: WebView onDispose callback")
                    }
                )
            }

visual demonstration:

https://github.com/user-attachments/assets/16f2c9dc-6b54-407d-966b-89a76fce1f5d

hellosagar avatar Jul 26 '25 20:07 hellosagar

Leider Nein! Ich bin aktuell am überlegen für Android die non-KMP version zu verwenden. https://github.com/KevinnZou/compose-webview

Rumpel1984 avatar Jul 27 '25 11:07 Rumpel1984

Leider Nein! Ich bin aktuell am überlegen für Android die non-KMP version zu verwenden. https://github.com/KevinnZou/compose-webview

That's sad :(

cc @KevinnZou Any response or help would be appreciated, Thanks

hellosagar avatar Jul 27 '25 12:07 hellosagar