Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android WebView performance

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());
like image 416
Usha Avatar asked Nov 02 '10 19:11

Usha


People also ask

Does Android System WebView improve performance?

Android webview is the system application of newer Android versions which helps in viewing webpages in other applications such as Twitter. You don't need to exit the application to open a link. Updating Android webview will fix the bugs in the app and will bring performance improvements as well.

How can I make my Android WebView faster?

I read about how to increase performance of WebView by implementing Caching web resources like JS, CSS and image files. You can also static resources in your native application, and by intercepting the Resource requests you can override the default behaviour of WebView.

Should I keep Android System WebView?

Google does not advise disabling WebView; users should keep it activated and updated. However, users running Android 7.0, 8.0 and 9.0 may want to disable it to conserve processing power or memory or avoid crashes related to update bugs.

Is Android WebView deprecated?

The Android system webview custom cache file has been deprecated and removed in Android 13. New apps and any app updates will now use the operating system default cache location.


3 Answers

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);
}
like image 109
George Maisuradze Avatar answered Oct 30 '22 16:10

George Maisuradze


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

like image 44
King of Masses Avatar answered Oct 30 '22 16:10

King of Masses


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);
}
like image 2
Ashok Goli Avatar answered Oct 30 '22 17:10

Ashok Goli