Search code examples
javascriptandroidevent-handlinghybrid-mobile-app

How to trigger (notify) a custom event on hardware back key press from native android to web view?


How to trigger an event from native android code on hardware back keypress which will notify Webview.

once the event is triggered, I will handle it in javascript like below.

document.addEventListener("backbutton", function(e){
   //callback
}

Solution

  • you may get the anwser from the code in cordova

    https://github.com/apache/cordova-android/blob/4a0a7bc424fae14c9689f4a8a2dc250ae3a47f82/framework/src/org/apache/cordova/CordovaWebViewImpl.java#L553-L596

    here is the docs https://cordova.apache.org/docs/en/latest/cordova/events/events.html