pierofrezza
pierofrezza

Reputation: 59

Back button in WebView not working correctly

I want to use the back button of Android to go back into a WebView (it's in a Fragment), so I put up this code in Kotlin:

catWebView.setOnKeyListener { _, keyCode, _ ->
    if (keyCode == KeyEvent.KEYCODE_BACK && catWebView.canGoBack()) {
        catWebView.goBack()
    }
    return@setOnKeyListener true
}

and that works, but with a problem: when I tap the back button, the WebView goes all the way back to the first page opened, and not by single steps...

How can I solve it? Thank you!

Upvotes: 0

Views: 2189

Answers (1)

Vijay S
Vijay S

Reputation: 418

Remove your setonkeylistener and Add

 @Override
    public void onBackPressed() {
        // super.onBackPressed(); Do not call super.onbackpressed!
  
    }

Upvotes: 2

Related Questions