Call on a destroyed WebView


I have a capacitor plugin that runs Android service. The service sends a broadcast message to the plugin and then plugin sends some event to application. Everything is OK until the application is closed and then run again. In such a situation I see the following stack trace in Android Studio:

W/cr_AwContents: Application attempted to call on a destroyed WebView
        at org.chromium.android_webview.AwContents.c(PG:3)
        at org.chromium.android_webview.AwContents.a(PG:188)
        at android.webkit.WebView.evaluateJavascript(
        at com.getcapacitor.MessageHandler$
        at android.os.Handler.handleCallback(
        at android.os.Handler.dispatchMessage(
        at android.os.Looper.loop(
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(

I have to add that application works as expected despite that. And I want to receive events when the application is in background (I mean: I don’t want to unregister from broadcast messages or plugin events).

It seems that every time the application is started a new instance of plugin is created (I’m loggin hashCode() on the plugin). The old plugin instance has a reference to non existing WebView and thus the error.

So now my question is: How and when to dispose old plugin?

If anybody has the same issue: Finally I remove listener when the application is deactivated and register when it’s activated. It needs a little bit more logic but seems to work correctly at least.