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());
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.
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.
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.
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.
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);
}
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
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With