title: Calling JavascriptInterface method through the WebView and JS function after changing config often doesnt work
body: <p>I have some problem with calling a JavaScript function in a WebView and getting response in the app (on my Galaxy Tab 2 '10). I call the js-function directly after content loading. According <a href="https://stackoverflow.com/a/4945875/968702">this one solution</a> i use a <code>PictureListener</code></p>
  
  <pre><code>webView.setPictureListener(new PictureListener() {
          @Override
          public void onNewPicture(WebView view, Picture picture) {
              webView.loadUrl("javascript: JsImpl.init('{response}');");
          }
  });
  </code></pre>
  
  <p>it works fine: when content is loaded, onNewPicture always is started.
  <code>"javascript: JsImpl.init('{response}')</code> loads js that calls:</p>
  
  <pre><code>class JsImpl {
      private final static String LOG_TAG = "JsImpl";
  
      @JavascriptInterface
      public void init(String json) {
          Log.d(LOG_TAG, json);
      }
  }
  </code></pre>
  
  <p>In the log i see <code>D/JsImpl(18400): {response}</code>. It seems to be everything okay but...</p>
  
  <p>When I switch my tab on a landscape or portrait mode the <code>WebView</code> is recreated, <code>onNewPicture</code> are started but the method <code>public void init(String json)</code> in the <code>JsImpl</code> is often not called at all!</p>
  
  <p>So if i change orientation, sometimes js works, sometimes doesn't work. No exception, no errors... </p>
  
  <p>Who faced with the same problem?</p>
  
  <p>Below is full code</p>
  
  <pre><code>public class NextActivity extends Activity {
  
  @SuppressLint("SetJavaScriptEnabled")
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_next);
  
      final WebView wv = (WebView) findViewById(R.id.WebView);
      wv.getSettings().setJavaScriptEnabled(true);
      wv.loadUrl("file:///android_asset/index.html");
      wv.addJavascriptInterface(new JsImpl(), "JsImpl");
  
      wv.setPictureListener(new PictureListener() {
          @Override
          public void onNewPicture(WebView view, Picture picture) {
              wv.loadUrl("javascript: JsImpl.init('{responce}');");
          }
      });
  }
  
  class JsImpl {
      private final static String LOG_TAG = "JsImpl";
  
      @JavascriptInterface
      public void init(String json) {
          Log.d(LOG_TAG, json);
      }
  }
  
  }
  </code></pre>
  
  <p>html code is not needed here, just put on the assets dir an empty html file <code>index.html</code>.</p>
  
  <hr>
  
  <p><strong>EDIT:</strong>
  I've found one soution but i dont know exactly is it correct varion. But it works.
  I added handler loop </p>
  
  <pre><code> final Handler handler = new Handler();
      handler.postDelayed(new Runnable() {
          @Override
          public void run() {
              if (isJSSetup) return;
              Log.i("handler-loop", "JS didn't handle. Reload page");
              wv.loadUrl(CONTENT_PAGE);
              handler.postDelayed(this, 1000);
          }
      }, 1000);
  </code></pre>
  
  <p>when @JavascriptInterface init(String json) is called, <code>isJSSetup</code> became <code>true</code>. In handler loop i check if JS is setup or no <code>if (isJSSetup) return;</code>. If no - i reload URL again wv.loadUrl(CONTENT_PAGE) and try to call init(String json). So long as it is not going to work. It's hardcode, so i'm still looking for a good solution.</p>