18

In my app, I am loading a list of external url's in webview and allow user to flip through them. Webviews are loaded on to a view flipper. I find the performance is really bad in webview load url. I have tried everything from using the frame layout to limiting the number of webviews to load. Still the performance is not satisfactory.

How do I optimize the performance of webview? This should be a common usage. Am I missing something obvious.

My Webview settings are -

    webView.setInitialScale(WEBVIEW_SCALE);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setBuiltInZoomControls(false);
    webView.setWebViewClient(new MyWebViewClient());    
    webView.setOnTouchListener( new OnTouchListener());
Usha
  • 181
  • 1
  • 1
  • 3
  • Are you sure it's not loading the data from the internet the problem? – Falmarri Nov 02 '10 at 20:07
  • Nope. Data is loaded. But it takes long time. – Usha Nov 02 '10 at 21:01
  • 1
    Loading multiple pages at a time is costly - even loading a single webpage is a costly operation. Putting aside all the processing of things like images, scaling of the view port etc. There are also limitations on performance of many concurrent socket connections. Finally I'd guess that if you watch the log you will see a lot of GC thrash. It is not really advisable to have many WebView's active at once. With out a quantitative description of "performance is not satisfactory" or the handset you are running on, I can only guess that there is not much you can do to improve it. – cistearns Nov 03 '10 at 00:06
  • I spent a decent amount of time trying to optimize the same sort of idea, and found that it really wasn't a good solution. There's just too much overhead between the page loads and viewflipper. In my case, I was able to get okay performance by loading each of the pages into one page sorted by CSS3 columns, and using fling to go between the columns. There's still a delay as the one big page gets rendered, but it was several times faster than loading each page into a viewflipper. That really wouldn't work with complicated html documents, though... mine just had text and a few images. – Turnsole Mar 06 '11 at 19:23

4 Answers4

4

Try this:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
    webView.setLayerType(View.LAYER_TYPE_HARDWARE, null); 
} else {
    webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}
George Maisuradze
  • 1,953
  • 19
  • 16
3

I think the following works best:

if (Build.VERSION.SDK_INT >= 19) {
    webView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
}       
else {
    webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}

Android 19 has Chromium engine for WebView. I guess it works better with hardware acceleration.

For more info Android 4.4 KitKat, the browser and the Chrome WebView

King of Masses
  • 18,405
  • 4
  • 60
  • 77
2

This has already been discussed here: Enhance webView performance (should be the same performance as native Web Browser)

I ran into a similar issue, and after some heavy debugging noticed the native browser and WebView browser seem to be using different caches.

This code can be used to disable the WebView cache, and made WebView much faster for me (though at the expense of not caching). Note that it uses private APIs, so by using it you're risking the code will break in future releases:

try
{
  Method m = CacheManager.class.getDeclaredMethod("setCacheDisabled", boolean.class);
  m.setAccessible(true);
  m.invoke(null, true);
}
catch (Throwable e)
{
  Log.i("myapp","Reflection failed", e);
}
Community
  • 1
  • 1
Ashok Goli
  • 5,043
  • 8
  • 38
  • 68
  • So this disables caching for every WebView used by the application? Would there be any way to disable caching for only a single WebView? Thanks! – Sven Viking Aug 23 '11 at 10:30
  • Looks as if my question may be answered by the WebSettings method `.setCacheMode(WebSettings.LOAD_NO_CACHE);` – Sven Viking Aug 25 '11 at 07:50
  • Although according to this question, it either doesn't work, or only skips the cache check but still saves to the cache. http://stackoverflow.com/questions/5239954/webviews-load-no-cache-setting-still-saves-files-to-the-disk – Sven Viking Aug 25 '11 at 08:02
  • 4
    I think this setting is for the second load of the page. I got this from the api for setCacheMode: The way the cache is used is based on the navigation option. For a normal page load, the cache is checked and content is re-validated as needed. When navigating back, content is not revalidated, instead the content is just pulled from the cache. This function allows the client to override this behavior. – Ashok Goli Aug 25 '11 at 09:09
  • 1
    I get a `java.lang.NoSuchMethodException` for `setCacheDisabled`. Maybe this function is deprecated and removed? – DBX12 Sep 26 '16 at 12:36
  • Disabling cache is a rip off. – Josh Sep 21 '17 at 07:52
0

Improvise Answer: The above Solution mentioned CacheManager.class is not supported.

 try {
            val m: Method = ServiceWorkerWebSettingsCompat.CacheMode::class.java.getDeclaredMethod(
                "setCacheDisabled",
                Boolean::class.javaPrimitiveType
            )
            m.isAccessible = true
            m.invoke(null, true)
        } catch (e: Throwable) {
            Log.i("myapp", "Reflection failed", e)
        }
Subratsss
  • 777
  • 6
  • 12