How to go back to previous page if back button is pressed in WebView?

后端 未结 17 1236
迷失自我
迷失自我 2020-11-22 07:06

I have an app in which I have a WebView where I display some websites. It works, clicking a link in the webpage goes to the next page in the website inside my a

17条回答
  •  鱼传尺愫
    2020-11-22 07:24

    Official Kotlin Way:

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        // Check if the key event was the Back button and if there's history
        if (keyCode == KeyEvent.KEYCODE_BACK && myWebView.canGoBack()) {
            myWebView.goBack()
            return true
        }
        // If it wasn't the Back key or there's no web page history, bubble up to the default
        // system behavior (probably exit the activity)
        return super.onKeyDown(keyCode, event)
    }
    

    https://developer.android.com/guide/webapps/webview.html#NavigatingHistory

提交回复
热议问题