Android Webview EvaluateJavascript sometimes does not return a response

Issue

I have been trying to fix this for weeks and have no clue what is causing the issue. In my project I am utilizing the Android Webview’s evaluateJavascript() method like this:

    this.runOnUiThread(new Runnable() {
        @Override
        public void run() {
            webView.evaluateJavascript(command, new ValueCallback<String>() {
                @Override
                public void onReceiveValue(String value) {
                    //Parsing and taking action here
                    Log.d("Response", value);
                }
            });
        }
    });

A sample string of the javascript I am sending would be:

document.getElementById("message").value"Stuff worked!"; 

or

document.getElementById("some-awesome-button").click(); 

While 9 times out of 10 these calls return a value in the onReceiveValue() method, once in a while I just straight never receive a response at all.

This lack of a response has made chaining events a nightmare and I have no clue why this would be happening.

Some more data to head off any additional questions:

  • The minimum SDK for this project is 21 and I am targeting 28.

  • I am not utilizing any javascriptInterfaces at this point in time and don’t intend to for this project for a few business-related reasons.

  • When I do get a response fro the webview in the onReceiveValue() method it is usually the value I had just set or ‘null’ if it was a click event. Regardless, the issue isn’t that I am sometimes receiving nulls or other values, but the distinct lack of a response sometimes.

  • As shown in the code sample, I am definitely running this on the UI Thread as per the documentation.

  • I had the server dev add in some code to console log whenever I am hitting the button and when I get back a successful value in the onReceiveValue() method the console logs are working and responding but when I am in the situation where I do not get back a response, the web console logs never fire and it never detects an interaction with the button.

  • I have also tried adding in a timer with logic that listens for a response from the webview and if it does not receive it, attempts to make the same call again up to 10x. When the first call fails, none of the subsequent attempts work.

  • These are the settings for my webview:

    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setDomStorageEnabled(true);
    webView.getSettings().setAllowFileAccess(true);
    

It almost seems like the issue is with the Javascript Bridge but I cannot be certain.

The summary question I have is, what could cause the Android WebView’s evaluateJavascript() call to not trigger a callback nor return a value in the onReceiveValue() method?

Solution

I did eventually figure out what happened here.

The shorter version is, this can happen when you have a memory leak and an object that is utilizing a semi-destroyed webview will fire off commands, but the newly-initialized webview will not receive a response because it is going to a dead listener.

If you want to prevent this from occurring, just make sure that all objects that maintain an application Context are properly disposed of or correctly reset if the activity restarts, else you have all objects that use the same Application-level context, but are using new views and callbacks entirely.

Answered By – PGMacDesign

Leave a Comment