4

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;
}
Whoami
  • 13,930
  • 19
  • 84
  • 140
naveenmethuku
  • 51
  • 1
  • 5
  • Please give some more information. What do you want to achieve? I did not get what you are asking. – Thomas R. Sep 18 '15 at 10:23
  • Is the webview is always visible ?? – uday Sep 18 '15 at 10:24
  • [Disable back button in android](https://stackoverflow.com/q/4779954/6521116) – LF00 Jun 23 '17 at 06:58
  • You can override either of these method `dispatchKeyEvent`, `onBackPressed`, `onKeyDown`. Refer to [this answer](https://stackoverflow.com/a/44714357/6521116) for more. – LF00 Jun 23 '17 at 07:16

6 Answers6

3

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();
   }
}
uday
  • 1,348
  • 12
  • 27
  • 1
    nice one! for me, I didn't ever want the webview to "steal" the back button press, so inside `public void onBackPressed()` I just had the one line `super.onBackPressed();` – Someone Somewhere Jul 24 '18 at 17:36
2

Simply override the onBackPressed() method.

@Override
public void onBackPressed() { }
KishuDroid
  • 5,411
  • 4
  • 30
  • 47
1

Please try this

   @Override
public void onBackPressed() {
      if(webview.canGoBack()){
         webview.goBack();
      }
    else{
      super.onBackPressed();
   }
}
Ashish Agrawal
  • 1,977
  • 2
  • 18
  • 32
1

There are many ways to make it,

Solution 1, overriding dispatchKeyEvent()

dispatchKeyEvent()(API Level 1, Android 1.0)

Refer to my answer use dispatchKeyEvent to disable back button

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    // TODO Auto-generated method stub
    if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
        return true;
    }
    return super.dispatchKeyEvent(event);
}

Solution 2, overriding onBackPressed()

onBackPressed() (API Level 5, Android 2.0)

Refer to Use onBackPressed() to disable back button

@Override
public void onBackPressed() {
}

Solution 3, overriding onKeyDown()

onKeyDown() (API Level 1, Android 1.0)

Refer to Use onKeyDown() to disable back button

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
     if (keyCode == KeyEvent.KEYCODE_BACK) {
     //preventing default implementation previous to android.os.Build.VERSION_CODES.ECLAIR
     return true;
     }
     return super.onKeyDown(keyCode, event);    
}
LF00
  • 27,015
  • 29
  • 156
  • 295
  • I want the browser to not perform the default back operation after the user presses the back key. Instead, JavaScript get a key event(`document.onkeydown` and the keyCode = 8). Is this possible? – forDream May 03 '18 at 13:51
  • @forDream I think it's possible, you need to make sure the js event or the java event first. And you may also pass the click event between js and java code. – LF00 May 03 '18 at 14:17
  • In general, I want to pass a keycode to webview for any value, such as keycode `-1` / `-2` or something else. I.e. trigger `document.onkeydown / onkeypress / onkeyup` – forDream May 03 '18 at 14:39
0

You have add below code in Activity for disable activity back pressed

@Override
public void onBackPressed() {

}
0
Add this below code in java file : 

WebView mwebView;
Button backButton1;

  backButton1 = findViewById(R.id.backButton1);
 mwebView = findViewById(R.id.mwebView);

backButton1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mWebView.destroy();
            }
        });
MEGHA DOBARIYA
  • 1,622
  • 9
  • 7