Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to separate WebView's UI and HTTP threads?

For various reasons, I need to runOnUiThread() the actual instantiation & initialization of WebView.

Which means that its underlying HTTP connections are also made on the UI thread?

If this is true, is it possible to separate the WebView's UI thead from the HTTP connections thread?

If it is possible, what is the proper way of accomplishing this?

like image 492
Bill The Ape Avatar asked Jun 27 '12 17:06

Bill The Ape


1 Answers

I find it very hard to believe that Android would run remote HTTP requests on the UI thread, assuming you initiate the requests via WebView.loadUrl(). This would make for a terrible user experience.

Just quickly, I ran an Android app in the debugger, with a basic WebView, and stopped in the debugger. Here's what I see:

enter image description here

If WebViewCoreThread, WebViewWorkerThread, or http0 - http3 aren't handling the network connections for WebView objects, then

  1. They have very bad names
  2. Android is built really badly

Also, if you look at this answer by one of stack overflow's highest reputation users, loadUrl() is asynchronous. A quick step through in the debugger tells me that loadUrl(), which is called on the UI thread (aka main), completes way too fast for the connection to be handled synchronously on the UI thread. (I put a breakpoint before and after the call to loadUrl() with a URL that I know isn't served very quickly).

So, my answer is you're done ... they're already separate! (yeah!)

like image 158
Nate Avatar answered Sep 27 '22 22:09

Nate