title: Add a window event listener to WebView and call a Java function
body: <p>I'm displaying a webpage in my Android app using a <code>WebView</code>, this webpage triggers a window message event and gives some data through that event. I need to access the data from the message event.</p>
  <p>To achieve this, I'm adding the JavaScript event listener by using <code>evaluateJavascript()</code> method and trying to call a Java function from the JavaScript using <code>addJavascriptInterface()</code>.</p>
  <p>My Code so far:</p>
  <pre><code>public class MyActivity extends AppCompatActivity {
      
      @Override
      @SuppressLint(&quot;SetJavaScriptEnabled&quot;)
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
      
          WebView webView = new WebView(this);
          webView.getSettings().setJavaScriptEnabled(true);
          webView.getSettings().setDomStorageEnabled(true);
      
          webView.addJavascriptInterface(new JsObject(this), &quot;Android&quot;);
      
          webView.evaluateJavascript(&quot;window.addEventListener('message', (e) =&gt; {Android.logData('Test String')})&quot;, null);
      
          webView.loadUrl(&quot;https://example.com/mypage&quot;);
      
          setContentView(webView);
      }
      
      private class JsObject {
          Context context;
      
          JsObject(Context context) {
              this.context = context;
          }
      
          @JavascriptInterface
          public void logData(String s) {
              Log.v(&quot;EEE&quot;, s);
          }
      }
      
  }
  </code></pre>
  <p>But the event handler is not getting called. I'm not sure whether this is the right approach. Please help. TIA.</p>