Call on a destroyed WebView

Hi,

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
    java.lang.Throwable
        at org.chromium.android_webview.AwContents.c(PG:3)
        at org.chromium.android_webview.AwContents.a(PG:188)
        at com.android.webview.chromium.WebViewChromium.evaluateJavaScript(PG:7)
        at android.webkit.WebView.evaluateJavascript(WebView.java:963)
        at com.getcapacitor.MessageHandler$1.run(MessageHandler.java:98)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:211)
        at android.app.ActivityThread.main(ActivityThread.java:5389)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1020)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815)

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.