Android Webview make back button go to previous page

前端 未结 4 659
遇见更好的自我
遇见更好的自我 2021-01-14 19:44

I have a web view that lets me browse through a site. When i click the back button, rather than it going to the previous page it exits the app. I have added the following me

相关标签:
4条回答
  • 2021-01-14 20:08

    Do not override onKeyDown event, override onBackPressed, so that every time the back button is pressed, you can override it and add your own logic into it. The Code is like this:

    @Override
    public void onBackPressed() {
        if (webView.canGoBack()) {
            webView.goBack();
        } else {
            super.onBackPressed();
        }
    }
    
    0 讨论(0)
  • 2021-01-14 20:10

    Full reference for next button and progress bar : put back and next button in webview

    If you want to go to back page when click on phone's back button, use this:

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

    You can also create custom back button like this:

    btnback.setOnClickListener(new View.OnClickListener() {
    
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
    
                if (wv.canGoBack()) {
                    wv.goBack();
                }
            }
        }); 
    
    0 讨论(0)
  • 2021-01-14 20:15

    Backkey pressed event is not suitable here use this code:

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        Intent intent=new Intent(LiveImage.this,DashBoard.class);
        startActivity(intent);
    }
    
    0 讨论(0)
  • 2021-01-14 20:27

    I think you should override your activity OnBackPressed :

    @Override
    public void onBackPressed() {
        if (webView.canGoBack()) {
            webView.goBack();
        } else {
            super.onBackPressed();
        }
    }
    
    0 讨论(0)
提交回复
热议问题