0

I have an app where the main activity (which extends ComponentActivity) is pretty much just a webView. I have overriden onBackPressed, shown below.

@Override
public void onBackPressed(){
  if(this.webView.canGoBack){
    this.webView.goBack();
  }
  else{
    super.onBackPressed();
  }
}

I did this so that when there is browser history, it should go through that instead of canceling the app itself. This works when running my app on my tablet which have android 11, but not on my phone with android 13. Now when onBackPressed is deprecated, I really don't understand how to replace this and keep the same behavior.

Tried using the onBackPressedDispatcher instead, but didn't succeed. Don't know if it's the wrong thing, or if I'm just handling it wrong.

1

1 Answer 1

-1

Maybe you could use the onKeyDown event?

override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {

    if (keyCode == KeyEvent.KEYCODE_BACK) {
        return true
    }

    return super.onKeyDown(keyCode, event)
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.