diff --git a/src/android/InAppBrowser.java b/src/android/InAppBrowser.java index 76613c789..2f8380705 100644 --- a/src/android/InAppBrowser.java +++ b/src/android/InAppBrowser.java @@ -160,7 +160,6 @@ public class InAppBrowser extends CordovaPlugin { */ public boolean execute(String action, CordovaArgs args, final CallbackContext callbackContext) throws JSONException { if (action.equals("open")) { - this.callbackContext = callbackContext; final String url = args.getString(0); String t = args.optString(1); if (t == null || t.equals("") || t.equals(NULL)) { @@ -171,6 +170,11 @@ public boolean execute(String action, CordovaArgs args, final CallbackContext ca LOG.d(LOG_TAG, "target = " + target); + final boolean keepCallback = !SYSTEM.equals(target); + if (keepCallback) { + this.callbackContext = callbackContext; + } + this.cordova.getActivity().runOnUiThread(new Runnable() { @Override public void run() { @@ -248,7 +252,7 @@ else if (SYSTEM.equals(target)) { } PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, result); - pluginResult.setKeepCallback(true); + pluginResult.setKeepCallback(keepCallback); callbackContext.sendPluginResult(pluginResult); } }); @@ -265,12 +269,6 @@ else if (action.equals("loadAfterBeforeload")) { @SuppressLint("NewApi") @Override public void run() { - if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.O) { - currentClient.waitForBeforeload = false; - inAppWebView.setWebViewClient(currentClient); - } else { - ((InAppBrowserClient)inAppWebView.getWebViewClient()).waitForBeforeload = false; - } inAppWebView.loadUrl(url); } }); @@ -541,6 +539,16 @@ public void onPageFinished(WebView view, String url) { dialog.dismiss(); dialog = null; } + + // Fix for webView window not being destroyed correctly causing memory leak + // (https://github.com/apache/cordova-plugin-inappbrowser/issues/290) + if (url.equals("about:blank")) { + inAppWebView.onPause(); + inAppWebView.removeAllViews(); + inAppWebView.destroyDrawingCache(); + inAppWebView.destroy(); + inAppWebView = null; + } } }); // NB: From SDK 19: "If you call methods on WebView from any thread @@ -1156,7 +1164,6 @@ public class InAppBrowserClient extends WebViewClient { EditText edittext; CordovaWebView webView; String beforeload; - boolean waitForBeforeload; /** * Constructor. @@ -1168,7 +1175,6 @@ public InAppBrowserClient(CordovaWebView webView, EditText mEditText, String bef this.webView = webView; this.edittext = mEditText; this.beforeload = beforeload; - this.waitForBeforeload = beforeload != null; } /** @@ -1229,7 +1235,7 @@ public boolean shouldOverrideUrlLoading(String url, String method) { } // On first URL change, initiate JS callback. Only after the beforeload event, continue. - if (useBeforeload && this.waitForBeforeload) { + if (useBeforeload) { if(sendBeforeLoad(url, method)) { return true; } @@ -1324,9 +1330,6 @@ else if (!url.startsWith("http:") && !url.startsWith("https:") && url.matches("^ } } - if (useBeforeload) { - this.waitForBeforeload = true; - } return override; }