androidwebviewsslerrorhandler

Go back to previous screen on SslErrorHandler


So in my webview, I am showing an error dialog box for SSL security issues which Google had made compulsory. The code looks like below:

        @Override
        public void onReceivedSslError(WebView view, final SslErrorHandler handler, SslError error)
        {
           final AlertDialog.Builder builder = new AlertDialog.Builder(LaunchWVActivity.this);              
           builder.setMessage("SSL cert is invalid.Install a valid certificate or click continue to proceed.");
           builder.setPositiveButton("continue", new DialogInterface.OnClickListener() 
           {                    
                    @Override
                    public void onClick(DialogInterface dialog, int which) 
                    {
                        handler.proceed();                  
                    }
            });
            builder.setNegativeButton("cancel", new DialogInterface.OnClickListener() 
                {                   
                    @Override
                    public void onClick(DialogInterface dialog, int which) 
                    {                           
                        handler.cancel();                           
                    }
            });
            final AlertDialog dialog = builder.create();    
            dialog.show();
}

When a use clicks cancel, my webview just stays on the same screen with white screen. I want to navigate user to previous screen from where he came from. I have tried adding a onCancelListener, but it has not worked.

Something like this

dialog.setCancelable(true);
dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
     @Override
     public void onCancel(DialogInterface dialog) {
           // dialog dismiss without button press
            Log.d(TAG,"Cancel pressed");
            webview.goBack();
}}); 

What am I missing here?


Solution

  • I resolved this issue. It was very straight forward approach with finishing the current activity. Initially I didn't realize, I could have my current activity as globally declared.

     builder.setNegativeButton("cancel", new DialogInterface.OnClickListener() 
                    {                   
                        @Override
                        public void onClick(DialogInterface dialog, int which) 
                        {                           
                            handler.cancel(); 
                            currentActivity.finish();                          
                        }
                });