2

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.

Kushal Sharma
  • 5,978
  • 5
  • 25
  • 41
akshay_shahane
  • 4,423
  • 2
  • 17
  • 30
  • The question is unclear. Can you elaborate a little? If you want to call the `setCache` methods like you mentioned through proteus you can add a custom attribute handler. – vader Feb 22 '17 at 06:33
  • I want to do operations on view once they are rendered through proteus like we do when xml inflate the view in this case webview using findviewbyid. in this case what is happening i am loading link of html store on our server in webview through proteus but when we chnage html webview load previous html due to caching i want disable that caching but how to do it when i cant use findviewbyid – akshay_shahane Feb 23 '17 at 06:53

1 Answers1

2

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.

vader
  • 889
  • 8
  • 22