48

Which one is faster way to load mobile web pages and non mobile web pages in Android webview; loading cache or not loading that at all?

And what is recommend style to load that?

Right now when I don't load cache at all non mobile sites are much more slower to load than when I load them in native browser.

Eljas
  • 1,187
  • 4
  • 17
  • 37

4 Answers4

56

Don't use these:

viewer.getSettings().setAppCacheMaxSize(1024*1024*8);   
viewer.getSettings().setAppCachePath("/data/data/com.your.package.appname/cache"‌​);    
viewer.getSettings().setAppCacheEnabled(true);   

These have nothing to do with the default webview internal cache. Appcache is an entirely different feature mean to make you able to run the website w/o an internet connection. It does not work that great and probably you do not want to use it.

With setting this: viewer.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT) is enough.

Community
  • 1
  • 1
nurieta
  • 1,615
  • 15
  • 6
  • 13
    Can you back this up with a link to some documentation describing AppCache? Given that `LOAD_DEFAULT` has the word "default" in it, surely the last line in your answer does nothing unless you've explicitly set a different value for it somewhere else? – Ed_ Jan 09 '14 at 12:19
  • 2
    setAppCacheMaxSize has been deprecated from api 18. http://developer.android.com/reference/android/webkit/WebSettings.html#setAppCacheMaxSize(long)] – r.bhardwaj May 21 '14 at 06:53
  • @ed-hinchliffe it's hard to find good documentation on this from Google itself, but they do refer to this document https://docs.webplatform.org/wiki/tutorials/appcache_beginner from the "Working offline" section of this post on the Google Chrome WebView: https://developer.chrome.com/multidevice/android/overview. Also you are correct in that his last line in the answer does nothing. – Nilzor Apr 16 '15 at 07:12
  • The default value is {@link #LOAD_DEFAULT} – Shubham AgaRwal Sep 19 '19 at 04:27
  • Would you explain why we shouldn't use those functions? I don't find warnings on the Android documentation. – aldok Feb 04 '20 at 07:38
  • webView.getSettings().setAppCacheEnabled(true); deprecated any alternate way to handle this? – Girish Mar 01 '21 at 11:03
  • the docs point to: https://web.dev/appcache-removal/ – Bruno Medeiros May 19 '21 at 09:54
15

Of course, cached approach should be faster. That's the exact reason caching is there in the first place.

But you should be fine unless you specifically disable caching for webview. If you don't - it will use cache by default.

Ivan Bartsov
  • 19,664
  • 7
  • 61
  • 59
  • 11
    Thank you. Is this smart style to cache or is there some problem with this? viewer.getSettings().setAppCacheMaxSize(1024*1024*8); viewer.getSettings().setAppCachePath("/data/data/com.your.package.appname/cache"); viewer.getSettings().setAppCacheEnabled(true); viewer.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT); – Eljas Feb 03 '12 at 23:10
  • 1
    Yeah, I don't see anything wrong with that (I assume you put real package name in your actual code :) ). If in doubt you can play around with size and do some tests so see which works best for the type of content you're loading. If the pages are big and you're planning to load a lot of different ones - might want a bigger cache than 1Mb. If they're mostly text and your app will only need to display a small number of them - you're probably fine with current settings. – Ivan Bartsov Feb 06 '12 at 12:58
  • before using AppCache, look at this: http://code.google.com/p/android/issues/detail?id=24180 – Oleksandr Feb 08 '12 at 18:16
  • http://developer.android.com/reference/android/webkit/WebSettings.html#setAppCacheEnabled(boolean) is for application cache api of html 5. It is deprecated and doesn't seem to play a role in webview cache unless implemented at the html page as well: http://developer.android.com/reference/android/webkit/WebSettings.html#setAppCacheEnabled(boolean) – Rajat Sharma Mar 08 '16 at 11:54
  • 3
    Yes don't hard code use this if it is a fragment: `getActivity().getApplicationContext().getCacheDir().getAbsolutePath()` – Shailendra Madda May 18 '17 at 17:07
2
    /*
        public abstract void setAppCacheEnabled (boolean flag)
            Sets whether the Application Caches API should be enabled. The default is false.
            Note that in order for the Application Caches API to be enabled, a valid database
            path must also be supplied to setAppCachePath(String).

        Parameters
            flag : true if the WebView should enable Application Caches
    */
    // Enable the caching for web view
    mWebView.getSettings().setAppCacheEnabled(true);

    /*
        public abstract void setAppCachePath (String appCachePath)
        Sets the path to the Application Caches files. In order for the Application Caches
        API to be enabled, this method must be called with a path to which the application
        can write. This method should only be called once: repeated calls are ignored.

        Parameters
            appCachePath : a String path to the directory containing Application Caches files.
    */
    /*
        public abstract File getCacheDir ()
            Returns the absolute path to the application specific cache directory on the
            filesystem. These files will be ones that get deleted first when the device runs
            low on storage. There is no guarantee when these files will be deleted.

            Note: you should not rely on the system deleting these files for you; you should
            always have a reasonable maximum, such as 1 MB, for the amount of space you consume
            with cache files, and prune those files when exceeding that space.

            The returned path may change over time if the calling app is moved to an adopted
            storage device, so only relative paths should be persisted.

            Apps require no extra permissions to read or write to the returned path,
            since this path lives in their private storage.

        Returns
            The path of the directory holding application cache files.
    */
    /*
        public String getPath ()
            Returns the path of this file.
    */
    // Specify the app cache path
    mWebView.getSettings().setAppCachePath(mContext.getCacheDir().getPath());

    /*
        public abstract void setCacheMode (int mode)
            Overrides the way the cache is used. The way the cache is used is based on the
            navigation type. For a normal page load, the cache is checked and content is
            re-validated as needed. When navigating back, content is not re-validated, instead
            the content is just retrieved from the cache. This method allows the client to
            override this behavior by specifying one of
                LOAD_DEFAULT,
                LOAD_CACHE_ELSE_NETWORK,
                LOAD_NO_CACHE or
                LOAD_CACHE_ONLY.
            The default value is LOAD_DEFAULT.

        Parameters
            mode : the mode to use
    */
    /*
        public static final int LOAD_DEFAULT
            Default cache usage mode. If the navigation type doesn't impose any specific
            behavior, use cached resources when they are available and not expired, otherwise
            load resources from the network. Use with setCacheMode(int).

        Constant Value: -1 (0xffffffff)
    */
    /*
        public static final int LOAD_CACHE_ELSE_NETWORK
            Use cached resources when they are available, even if they have expired. Otherwise
            load resources from the network. Use with setCacheMode(int).

        Constant Value: 1 (0x00000001)
    */
    /*
        public static final int LOAD_NO_CACHE
            Don't use the cache, load from the network. Use with setCacheMode(int).

        Constant Value: 2 (0x00000002)
    */
    /*
        public static final int LOAD_CACHE_ONLY
            Don't use the network, load from the cache. Use with setCacheMode(int).

        Constant Value: 3 (0x00000003)
    */
    // Set the cache mode
    mWebView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
Masood
  • 909
  • 7
  • 11
1
 mWebView.getSettings().setAppCacheEnabled(true);

Here setAppCacheEnabled(true) is deprecated in Target SDK 33.

Use settings.cacheMode = WebSettings.LOAD_DEFAULT instead.

ASP
  • 3,645
  • 1
  • 31
  • 45
  • I am getting error here `webView.getSettings().setAppCachePath(Path)`, How to set path in API level 33? – Ninja Aug 14 '23 at 07:29