0

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?

Malhotra
  • 221
  • 3
  • 13
  • have you try startActivityForResult https://stackoverflow.com/questions/37768604/how-to-use-startactivityforresult – Amit pandey Mar 10 '21 at 07:56

1 Answers1

0

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".

David Wasser
  • 93,459
  • 16
  • 209
  • 274