Home Development for Android Single threading in Android WebView

Single threading in Android WebView

by admin

Background

While developing a mobile application for a large international company, it was necessary to display a Web page in WebView. The page is in Java Script and contains DOM content about 10-15 MB. Due to the big size the page takes about 30-50 seconds to load depending on the internet connection. These results are not happy, so it was necessary to thoroughly investigate this problem.

Caching in WebView

The only solution I found was to use caching for downloadable content :

webview.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);

The download speed has doubled. However, to keep content current, WebView checks each file individually for validity. Although the speed has increased, the loading time of this page within WebView compared to the Chrome mobile browser was inferior by several times.

Single-threaded WebView

After careful research I came to the conclusion that WebView uses only one thread for loading data. This is true for both WebView with native WebKit (SDK< 19) and WebView with Chromium engine (SDK> =19). Chrome, in contrast to WebView, uses many threads (the number depends on the processor) and does not use WebView. So comparing WebView with Chrome is like comparing a Zaporozhets with a Lamborghini, it makes no sense. The developers of the native Cromium component for WebView are not supposed to implement multi-threading and believe that a single thread guarantees stable operation.
Let’s not forget that the platform is oriented to use third-party applications which the user himself chose for some qualities of his own.

Intent browserIntent = new Intent(Intent.ACTION_VIEW, uri);startActivity(browserIntent);

But within the scope of my project this did not suit the customer. So it was decided to leave WebView with all its shortcomings. Beforehand I had to prove that other large projects would not work with the original page as well:
Twitter refused to open the page and immediately offered to use the browser.
2. Facebook offered to use a browser, but still opened the page in WebView at the same speed as my solution.
Luckily that was enough and the pressure stopped on me.

Output

You should not use WebView to display large, complex pages. Recommended use of WebView:
1. For authorization on any services.
2. To display simple pages.
3. To align text to width (Standard TextView doesn’t have this feature, but that’s another story).
Let’s not forget that Android uses the Intent system, thanks to which we can use third-party browsers, which are much faster.

You may also like