androidjsonwebviewflipkart-apiproteus

Is there any way disable caching in webview inflated through proteus


Need to disable the cache on WebViews inflated through Proteus.

Are there any attributes on the WebView which can be used to disable it?

We could find the view normally would if it was inflated using precompiled XML layouts using findViewById(R.id.something) and call the following methods on it.

WebView wv = parent.findViewById(R.id.webview);
WebSettings ws = wv.getSettings();

ws.setAppCacheEnabled(false); 
ws.setCacheMode(WebSettings.LOAD_NO_CACHE)

But since proteus inflates layouts using JSON from the server I cannot find the view like this and the solution would not scale for multiple WeViews.


Solution

  • You can register a custom attribute handler say disableCache for the WebView and solve your problem.

    ProteusBuilder builder;
    builder.register("WebView, "disableCache", new BooleanAttributeProcessor<WebView>() {
         @Override
         public void setBoolean(WebView view, boolean value) {
             WebSettings ws = view.getSettings();
             if (value) {
                 ws.setAppCacheEnabled(value); 
                 ws.setCacheMode(WebSettings.LOAD_NO_CACHE);
             }
         }
    });
    Proteus proteus = builder.build();
    ProteusContext context = proteus.createContextBuilder(context).build();
    
    layoutInflater = context.getInflater();
    

    Use layoutInflater to inflate your layout and then in your layout set the attribute to true or false as you please.

    {
      "type": "WebView",
      "disableCache": "true"
    }
    

    You can register the custom attribute handler with your instance of Proteus and it will start working. The benefit is that you can toggle the flag based on your requirements and avoid hard coding cache disabled for all web views inflated through proteus.