Cap-go / capacitor-inappbrowser

Capacitor plugin in app browser with urlChangeEvent
MIT License
66 stars 48 forks source link

InAppBrowser.close() closes the entire application instead of just the WebView #87

Closed Senzo13 closed 12 months ago

Senzo13 commented 1 year ago

I am currently using the capacitor-inappbrowser plugin to open URLs within a WebView in my application. While the InAppBrowser.openWebView() method works as expected and opens the URL correctly, I am encountering an issue when trying to close the WebView.

Steps to reproduce:

Use InAppBrowser.openWebView() to open a URL. Add a listener for the urlChangeEvent to capture URL changes. Once the URL change event is captured, use InAppBrowser.close() to close the WebView. Expected Behavior: The WebView should close, and the user should return to the main application.

Actual Behavior: The entire application closes instead of just the WebView.

Additional Information:

The listener for the urlChangeEvent correctly captures the event and the URL. The issue persists across multiple attempts and seems consistent.

Framework : Angular 16 / Ionic 7.4
Platform : Android
Simulator : PIxel 6a API 33

I would appreciate any guidance or fixes for this issue. Thank you!

mohanmuthusamy commented 12 months ago

I am getting the same issue above. Does anyone help with this? And the error is

Caused by: java.lang.Throwable: A WebView method was called on thread 'CapacitorPlugins'. All WebView methods must be called on the same thread. (Expected Looper Looper (main, tid 2) {b5b07c7} called on Looper (CapacitorPlugins, tid 91) {153e440}, FYI main Looper is Looper (main, tid 2) {b5b07c7})

slavsobuzz commented 12 months ago

Hello, I encounter the same error which is very blocking in the progress of the development of my application. Hoping that someone has managed to resolve this issue.

Best regards.

Senzo13 commented 12 months ago

I fixed the problem temporarily this way

When using the close method of the InAppBrowser plugin, instead of just closing the WebView, the entire activity is being closed. This behavior is observed when the line notifyListeners("closeEvent", new JSObject().put("url", webViewDialog.getUrl())); is active. Commenting out this line seems to resolve the issue.

Theory:

It's possible that the "closeEvent" is being handled in a specific way on the JavaScript side that triggers the activity to close. When the event is emitted by notifyListeners, the event handler on the JavaScript side might be executing some code that results in the activity closing or other unintended behaviors.

Recommendation:

Check how the "closeEvent" is being handled in your JavaScript code. Ensure no code is closing the activity or causing unintended behavior. If the JavaScript event handler is in order, there might be an issue with how Capacitor handles events between native code and JavaScript. In this case, further investigation would be needed.

Commented line to temporarily resolve the issue image

If the creator of capgo agrees, I can offer my npm package temporarily

mohanmuthusamy commented 12 months ago

@Senzo13 You are doing a great job! It worked after I commented on the line you highlighted earlier.

mohanmuthusamy commented 12 months ago

@Senzo13 I closed the web view, and it routed to another page via code( this.router.navigate), but it did not redirect to another page. It still stays the same. Is there any reason?

Senzo13 commented 12 months ago

@mohanmuthusamy When you close the WebView and attempt to navigate, are you redirected back to the previously closed WebView? Which technology are you using? I'm working with Angular and Ionic.

For me, when I navigate elsewhere, I don't encounter any issues. However, I've noticed a lingering afterimage of the WebView in the browser's hidden URLs, even after I've removed the listeners on my end. If you haven't already, I'd recommend using the removeAllListeners method to ensure all listeners are properly removed.

mohanmuthusamy commented 12 months ago

@Senzo13 I'm also using Ionic angular, and this is my code and its closing the webview but its failed to routing

image
riderx commented 12 months ago

Hello guys sorry for the issue, it was not closing the app but crashing it, the close event needed to read the current url and that has to be in the main thread. please try the 1.2.13 it should be fixed, sorry for this