Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable back button pressed for webview in android?

How to disable back button pressed for webview in android ?

@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
     if (wv1 != null && (keyCode == KeyEvent.KEYCODE_BACK)
                     && wv1.canGoBack() )
     {

        wv1.goBack();
    }

    return true;
}
like image 329
naveenmethuku Avatar asked Mar 15 '23 17:03

naveenmethuku


1 Answers

If you want to disable back button action when the WebView Visible and enable back button action if the WebView in not Visible try the below code in your Activity

@Override
public void onBackPressed() {
   if(webview.getVisibility()==View.VISIBLE){
      // dont pass back button action
      if(webview.canGoBack()){
         webview.goBack();
      }
      return;
   }else{
      // pass back button action
      super.onBackPressed();
   }
}
like image 183
uday Avatar answered Apr 06 '23 23:04

uday