Posting on UI thread from javascript interface
<p>I am trying to post a Runnable to be executed on the main thread, from a javascript interface method.
I do it in three different ways which I believe all should work.
But in fact only two of them work. 
For some reason, View.post(Runnable) does not work.
I want an explanation why.</p>

<p>This is the source:</p>

<pre><code>package com.package1.test;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.WebSettings;
import android.webkit.WebView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        WebView webView = new WebView(this);
        WebSettings ws = webView.getSettings();

        ws.setJavaScriptEnabled(true);
        webView.addJavascriptInterface(new JavaObject(),"obj");
        setContentView(webView);
        webView.loadUrl("javascript:obj.test(0); ");
    }

    static class Runner implements Runnable {
        private String msg;

        public Runner(String msg) {
            this.msg = msg;
        }

        @Override
        public void run() {
            String name = Thread.currentThread().getName();
            Log.i("Test","Execute on thread "+name+": "+msg);
        }
    }

    class JavaObject {
        private View view;

        public JavaObject() {
            this.view = new View(MainActivity.this);
        }

        @JavascriptInterface
        public void test(final int value) {
            Log.i("Test","called on "+Thread.currentThread().getName()+" thread");
            doOnMainThread1(new Runner("Method 1"));
            doOnMainThread2(new Runner("Method 2"));
            doOnMainThread3(new Runner("Method 3"));
        }

        public void doOnMainThread1(Runnable r) {
            view.post(r);
        }

        public void doOnMainThread2(Runnable r) {
            new Handler(Looper.getMainLooper()).post(r);
        }

        public void doOnMainThread3(Runnable r) {
            MainActivity.this.runOnUiThread(r);
        }

    }
}
</code></pre>

<p>This is the log output:</p>

<pre><code>08-31 19:34:02.703 22359-22419/com.package1.test I/Test: called on JavaBridge thread
08-31 19:34:02.771 22359-22359/com.package1.test I/Test: Execute on thread main: Method 2
08-31 19:34:02.772 22359-22359/com.package1.test I/Test: Execute on thread main: Method 3
</code></pre>