Skip to content

Fix memory leak #31

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 17 additions & 14 deletions src/android/InAppBrowser.java
Original file line number Diff line number Diff line change
@@ -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;
}