Android webview after onJsAlert not responding taps

I just faced the exactly same problem. I wanted to generate a custom android dialog instead of the alert, to this is the final solution:

myWebView.setWebChromeClient(new WebChromeClient() {
    @Override
    public boolean onJsAlert(WebView view, final String url, String message,
            JsResult result) {

        AlertDialog.Builder builder = new AlertDialog.Builder(
                MainActivity.this);
        builder.setMessage(message)
                .setNeutralButton("OK", new OnClickListener() {
                    @Override
                    public void onClick(DialogInterface arg0, int arg1) {
                        arg0.dismiss();
                    }
                }).show();
        result.cancel();
        return true;
    }
});

The key is the result.cancel(); and return true;, I tested in several combinations, and the only one that did not fired the default JS alert AND didn't caused the touch problem was this combination


I also faced the similar problem (but with onJsPrompt in my case) and the suggested solution did not work for me. I have already had the calls to result.cancel()/result.confirm() and return true from the handler. The key for the fix was found in the source code of JsDialogHelper.

This was the line which I spotted to solve my issue:

builder.setOnCancelListener(new CancelListener());

The complete handler code:

  @Override
  public boolean onJsPrompt(WebView view, String url, String message, String defaultValue, final JsPromptResult result)
  {
    final EditText data = new EditText(view.getContext());
    AlertDialog.Builder b = new AlertDialog.Builder(view.getContext())
    .setTitle(view.getTitle())
    .setView(data)
    .setMessage(message)
    .setOnCancelListener(new CancelListener(result)) // if this line is missing, WebView remains unresponsive after the dialog is shown and closed once
    .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener()
    {
      @Override
      public void onClick(DialogInterface dialog, int which)
      {
        result.confirm(data.getText().toString());
      }
    })
    .setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener()
    {
      @Override
      public void onClick(DialogInterface dialog, int which)
      {
        result.cancel();
      }
    });

    b.show();

    return true;
  }

where CancelListener can be defined as a simple stub class:

private class CancelListener implements DialogInterface.OnCancelListener,
DialogInterface.OnClickListener
{
  CancelListener(JsResult result)
  {
    mResult = result;
  }

  private final JsResult mResult;

  @Override
  public void onCancel(DialogInterface dialog)
  {
    mResult.cancel();
  }

  @Override
  public void onClick(DialogInterface dialog, int which)
  {
    mResult.cancel();
  }
}      

I'm not sure if this is a bug in WebView, or is it required to always have a cancel listener defined to keep webview's dialogs working properly.


you should call result.confirm() method, my solution is :

        public boolean onJsAlert(WebView view, String url, String message,
                final JsResult result) {
            Log.i("MainActivity", "onJsAlert url=" + url + ";message=" + message);
            Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
            result.confirm();
            return true;
        }