How to disable back button pressed for webview in android?

前端 未结 6 769
时光说笑
时光说笑 2020-12-19 15:38

How to disable back button pressed for webview in android ?

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


        
相关标签:
6条回答
  • 2020-12-19 16:10

    Simply override the onBackPressed() method.

    @Override
    public void onBackPressed() { }
    
    0 讨论(0)
  • 2020-12-19 16:14

    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);    
    }
    
    0 讨论(0)
  • 2020-12-19 16:17

    Please try this

       @Override
    public void onBackPressed() {
          if(webview.canGoBack()){
             webview.goBack();
          }
        else{
          super.onBackPressed();
       }
    }
    
    0 讨论(0)
  • 2020-12-19 16:18

    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();
       }
    }
    
    0 讨论(0)
  • 2020-12-19 16:25
    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();
                }
            });
    
    0 讨论(0)
  • 2020-12-19 16:32

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

    @Override
    public void onBackPressed() {
    
    }
    
    0 讨论(0)
提交回复
热议问题