简体   繁体   English

Jetpack Compose WebView 处理返回导航和 Go 到上一页

[英]Jetpack Compose WebView Handling Back Navigation And Go To Previous Page

I am using Jetpack Compose and have a WebView wrapped in a AndroidView composable that looks like the following:我正在使用 Jetpack Compose,并有一个WebView包装在一个AndroidView可组合组件中,如下所示:

AndroidView(modifier = modifier, factory = { context ->
        WebView(context).apply {
            layoutParams = ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT
            )
            webViewClient = WebViewClient()
            settings.javaScriptEnabled = true
        }
    }, update = { webView -> webView.loadUrl(url) })

In the legacy way, we could add a OnBackPressedDispatcher to the Activity to intercept the back press and navigate inside the WebView by accessing it via viewBinding for example with functions of the WebView like goBack() and to check if you can go back with canGoBack() . In the legacy way, we could add a OnBackPressedDispatcher to the Activity to intercept the back press and navigate inside the WebView by accessing it via viewBinding for example with functions of the WebView like goBack() and to check if you can go back with canGoBack() .

But how can we achieve the same with this Jetpack Compose approach?但是我们如何使用这种 Jetpack Compose 方法实现相同的目标呢?

There doesn't seem to be anything wrong with assigning the WebView to an external var so that's what I've done here.将 WebView 分配给外部变量似乎没有任何问题,所以这就是我在这里所做的。

var backEnabled by remember { mutableStateOf(false) }
var webView: WebView? = null
AndroidView(
    modifier = modifier,
    factory = { context ->
        WebView(context).apply {
            layoutParams = ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT
            )
            webViewClient = object : WebViewClient() {
                override fun onPageStarted(view: WebView, url: String?, favicon: Bitmap?) {
                    backEnabled = view.canGoBack()
                }
            }
            settings.javaScriptEnabled = true

            loadUrl(url)
            webView = this
        }
    }, update = {
        webView = it
    })

BackHandler(enabled = backEnabled) {
    webView?.goBack()
}

The WebViewClient listens for onPageStarted which checks if the WebView can navigate backwards and then updates backEnabled . WebViewClient侦听onPageStarted ,它检查 WebView 是否可以向后导航,然后更新backEnabled This causes a recomposition which toggles the BackHandler on and off.这会导致重新组合打开和关闭BackHandler

I've also moved loadUrl out of the update function and into factory because update is called every time there's a recomposition whereas factory is only called the once.我还将loadUrlupdate function 中移出并移至factory ,因为每次重组时都会调用更新,而工厂只调用一次。 This may or may not be relevant based on your implementation.根据您的实施,这可能相关也可能不相关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在使用 Jetpack Compose 的导航组件时,弹出由可组合表示的屏幕(从“返回堆栈”)返回上一个屏幕? - Popping off screen (from “back stack”) represented by a composable to go back to previous screen while using navigation component of Jetpack Compose? 如果在WebView中按了Back,则返回上一页 - Go Back To Previous Page if Back is Pressed in WebView Android-WebView moveTaskToBack返回上一页 - Android - WebView moveTaskToBack to go back to the previous page 在 Jetpack Compose 中处理背压 - Handling back press in Jetpack Compose "在 Jetpack Compose 中实现后退导航" - Implementing back navigation in Jetpack Compose 如果在WebView片段中按下“后退”按钮,如何返回上一页? - How to go back to previous page if back button is pressed in WebView fragment? go WebView 按下后退键如何回到上一页? - How to go back to previous page if back button is pressed in WebView? 导航抽屉,处理后退按钮转到上一个片段? - Navigation drawer, handling the back button to go to previous fragments? Android Webview 让后退按钮转到上一页 - Android Webview make back button go to previous page 如何使用Navigation Drawer和webview在应用程序中启用返回上一页 - How to enable back to previous page in a app with Navigation Drawer and webview
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM