Question: Compose WebView - avoid closing application when I press button back, but save ability go back in web view

I am experiencing an issue with the BackButton in a Compose WebView. The current code works well with the WebView backstack. However, when the backstack is empty and the back button is pressed, it causes the application to close, which is not the desired behavior.
@Composable
fun StatScreen(zeusPrefManager: ZeusPrefManager){

    var fPCallback: ValueCallback<Array<Uri>>? by remember {
        mutableStateOf(null)
    }

    val launcher = rememberLauncherForActivityResult(
        contract = ActivityResultContracts.GetMultipleContents()
    ) {
        fPCallback!!.onReceiveValue(it.toTypedArray())
    }

    var backEnabled by remember { mutableStateOf(false) }
    var webView: WebView? = null


    var gameStat by remember {
        mutableStateOf("")
    }

    LaunchedEffect(key1 = "goStat"){
        val destination = zeusPrefManager.zeusStatDataFlow.first()
        gameStat = destination
    }

    if (gameStat != ""){
        AndroidView(factory = { context ->
            WebView(context).apply {
                layoutParams = ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT
                )
                settings.javaScriptEnabled = true
                settings.domStorageEnabled = true
                settings.loadWithOverviewMode = true
                settings.userAgentString = ZeusConstan.converter007(settings.userAgentString)

                webViewClient = object: WebViewClient(){

                    override fun onPageStarted(view: WebView, url: String?, favicon: Bitmap?) {
                        backEnabled = view.canGoBack()
                    }

                    override fun onPageFinished(view: WebView?, url: String?) {
                        super.onPageFinished(view, url)

                    }
                }

                webChromeClient = object : WebChromeClient(){
                    override fun onShowFileChooser(
                        webView: WebView?,
                        filePathCallback: ValueCallback<Array<Uri>>?,
                        fileChooserParams: FileChooserParams?
                    ): Boolean {

                        if (fPCallback != null){
                            fPCallback!!.onReceiveValue(null)
                        }
                        fPCallback = filePathCallback
                        launcher.launch("image/*")
                        return true
                    }
                }
                loadUrl(gameStat)
            }
        }, update = {
            webView = it
        })

        BackHandler(enabled = backEnabled) {
            if (webView!!.canGoBack()){
                webView?.goBack()
            } else {
                //Do nothing but webView!!.canGoBack() is always true, and close the app
            }

        }
    }
}

I didn't override onBackPressed() in Activity.
I need to avoid closing application when I press button back


Answer:
you can make use of the onBackPressedDispatcher in your activity. Change your code as below-
    @Composable
fun StatScreen(zeusPrefManager: ZeusPrefManager, onBackPressed: () -> Unit) {
    
    BackHandler(enabled = backEnabled) {
        if (webView!!.canGoBack()) {
            webView?.goBack()
        } else {
            onBackPressed()
        }
    }
}

In your activity, override the onBackPressed() method and pass it to the StatScreen composable function:
override fun onBackPressed() {
    // Handle the back button press here
}