How to go back to previous page if back button is pressed in WebView?

后端 未结 17 1110
迷失自我
迷失自我 2020-11-22 07:06

I have an app in which I have a WebView where I display some websites. It works, clicking a link in the webpage goes to the next page in the website inside my a

17条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-22 07:29

    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();
                }
            }
        }); 
    

提交回复
热议问题