JavascriptInterface was called but the toast was not displayed

JavascriptInterface was called but the toast was not displayed … here is a solution to the problem.

JavascriptInterface was called but the toast was not displayed

I have a webview that ignores all of my JavaScript interface methods, or at least it seems. When I run the website from a normal browser, it throws an error because the interface is not defined as expected, but this leads me to believe that at least the interface method is called, but there are other problems. Can anyone help me figure out what?

Statement Content

    @Override
    protected void onPostExecute(ValidationResult validation)
    {
        if (validation.wasSuccessful)
        {
            URL = validation.message;
            wv = (WebView) findViewById(R.id.webView);              
            wv.addJavascriptInterface(new KioskInterface(wv.getContext()), "Android");
            WebSettings ws = wv.getSettings();
            ws.setJavaScriptEnabled(true);
            wv.loadUrl(URL);
        }
        else
        {
            errorAlert(title, msg + "\nContact network management.");
        }
    }

Kiosk interface class

public class KioskInterface {
    Context c;

KioskInterface(Context context)
    {
        c = context;
    }

@JavascriptInterface
    public void showToast()
    {
        Toast.makeText(c, "Hiding Keyboard", Toast.LENGTH_LONG).show();
    }
}

Javascript

function example() {
    Android.showToast();
}

I

never see any toasts, but I also don’t receive any false or corrupted behavior.

Solution

It turns out that clearing the cache is important, even if you are installing a new version of the app.

Related Problems and Solutions