How can I call Android JavaScript functions in WebView?

When I call a Java applet function by reference from JavaScript code, it doesn't work correctly.   Is it possible to call Java functions by reference from JavaScript? My guess: I think because Java takes memory on JVM, it is not possible.

  • // Java Code:     public class  A extends Applet{             public void foo(int[] a)         {             a[0]=3;         }     }         //Javascript Code:     var arg = new Array(1);     arg[0]=2;     Applet.foo(arg); But `arg[0]` remains `2`

  • Answer:

    You are right, JavaScript arrays are copied and converted before they are passed to Java Code. From the https://jdk6.java.net/plugin2/liveconnect/#ARRAY_ACCESS specification: "If a Java method takes an array type as argument, JavaScript may pass a JavaScript array object or literal for that argument. The Java Plug-In will allocate a new Java array and convert each element of the JavaScript array to the appropriate Java type using the conversion rules [...]" If, on the other hand, you allocate the array in Java, you should be able to use it by reference (I didn't try this myself, but the specification above says it).

David Tanzer at Quora Visit the source

Was this solution helpful to you?

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.