Android javascript bridge and anonymous functions
<p>I'm doing some Android/javascript bridging on a project using <a href="http://developer.android.com/reference/android/webkit/WebView.html#addJavascriptInterface%28java.lang.Object,%20java.lang.String%29" rel="nofollow">addJavascriptInterface()</a>.  I'm wondering how would this work with javascript anonymous callback functions?</p>

<p>For example:</p>

<pre><code>&lt;html&gt;  
&lt;head&gt;  
    &lt;script type="text/javascript"&gt;          
        function CallbackMethod(message){  
          ...
        }

    &lt;/script&gt;  
&lt;/head&gt;  
&lt;body&gt;  
    &lt;div id="te"&gt;&lt;/div&gt;
    &lt;button onClick="Android.TestMethodDataCallback('data', 'CallbackMethod');"&gt;Pass data and callback&lt;/button&gt;
&lt;/body&gt;  
&lt;/html&gt;
</code></pre>

<p>In native code, I can handle this fine by grabbing the name of the callback function and invoking that function directly by name:</p>

<pre><code>    webView.addJavascriptInterface(this, "Android");
    ...

    public void TestMethodDataCallback(String s, String cb)
    {
        String js = "javascript:" + cb + "('cb method called');";
        webView.loadUrl(js);
    }
</code></pre>

<p>But suppose the html TestMethodCallback line changed to:</p>

<pre><code>&lt;button onClick="Android.TestMethodDataCallback('data', function(message){...});"&gt;Pass data and callback&lt;/button&gt;
</code></pre>

<p>Is there a way to handle this in Android?  I can't call <code>'javascript:&lt;method_name&gt;'</code> anymore since it's now an anonymous function where as before the function was named <code>CallbackMethod</code>.</p>
