How to clear Webview Cache from Another activity

657 Views Asked by At

I have a fullscreen Webview in Activity A and I have a clear button on Activity B to clear the Webview cache. How can I call that Webview clear functionality from another activity?

Tried the below scenario but it crashed because Webview will be null while calling this function from Activity B

   public void clearCacheWeb()
    {

        mWebview.clearCache(true);
        mWebview.clearHistory();
        mWebview.clearFormData();
    }

Is there any hope for this?

1

There are 1 best solutions below

4
David Wasser On

In ActivityA register a BroadcastReceiver that has an IntentFilter listening for ACTION="clear-cache". In the BroadcastReceiver method onReceive() you can clear the cache in the WebView.

In ActivityB, to clear the cache, send a local broadcast Intent with ACTION="clear-cache".