Even without relying on the utilities provided by Capacitor, you can easily pass data back and forth between your web app and its native counterpart:
➡️In JavaScript / TypeScript you can register global functions on the window object. The native code can call these via executeJavaScript() on the WebView.
➡️In the other direction, the native code can register a JavaScript interface on the
WebView:
webView.addJavascriptInterface(new Object() {
@JavascriptInterface
public void onSessionId(String value) {
SaveUrlHandler.instance.setSessionId(value);
}
@JavascriptInterface
public void closeApp() {
MainActivity.this.finish();
}
}, "Android");
which the JavaScript / TypeScript code can call whenever it wants:
handleBackButton() {
if (this.location.isCurrentPathEqualTo('/')) {
if ((window as any)['Android']) {
(window as any)['Android'].closeApp();
}
...
}