title: Executing JavaScript instructions after loading HTML file in a WebView
body: <p>I realized an Android program that loads a HTML file into a WebView, the HTML file loads Leaflet.js that shows different styles of maps based on the provider you gave it.</p>
  
  <p>I load the page in the OnCreate() method with this instruction:</p>
  
  <pre><code>myWebView.loadUrl("file:///android_asset/test.html");
  </code></pre>
  
  <p>After that, I need to take the list of different maps from a Handler (with class name JavascriptInterface.java) that I added to the WebView like this:</p>
  
  <pre><code>myWebView.addJavascriptInterface(new JavaScriptInterface(this, myWebView), "MyHandler");
  </code></pre>
  
  <p>The final code needs to look like this:</p>
  
  <pre><code>myWebView.loadUrl("file:///android_asset/test.html");
  myWebView.loadUrl("javascript:window.MyHandler.getMaps()");
  </code></pre>
  
  <p>The problem is that the first loadUrl() isn't fast enough, so the second loadUrl goes before the map is initialized, causing a lot of problems.</p>
  
  <p>The temporary, and also horrible, solution that I found is to hold the second loadUrl for 1 second before executing it, like this:</p>
  
  <pre><code>myWebView.loadUrl("file:///android_asset/test.html");
  
  Handler handler = new Handler();
  handler.postDelayed(new Runnable() {
      public void run() {
          // Actions to do after 1 second
          myWebView.loadUrl("javascript:window.MyHandler.getMaps()");
      }
  }, 1000);
  </code></pre>
  
  <p>There is a better way of executing an instruction after another one? I already tried using an ASyncTask, but there is the possibility that I didn't implemented it well.</p>