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

后端 未结 17 834
迷失自我
迷失自我 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:44

    here is a code with confirm exit:

    @Override
        public void onBackPressed()
        {
            if(webView.canGoBack()){
                webView.goBack();
            }else{
                new AlertDialog.Builder(this)
                .setIcon(android.R.drawable.ic_dialog_alert)
                .setTitle("Exit!")
                .setMessage("Are you sure you want to close?")
                .setPositiveButton("Yes", new DialogInterface.OnClickListener()
                {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        finish();    
                    }
    
                })
                .setNegativeButton("No", null)
                .show();    
            }   
        }
    

提交回复
热议问题