Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Cordova event


May 21, 2021 Cordova


Table of contents


Various events can be used in Cordova projects. /b10> The following table shows the available events.

Cordova event

Serial number Events and descriptions
1

deviceReady

Once Cordova is fully loaded, the event is triggered. /b10> This helps ensure that the Cordova function is not called before everything is loaded.

2

pause

The event is triggered when the application enters the background.

3

resume

Events are triggered when the application returns from the background.

4

backbutton

The event is triggered when the return button is pressed.

5

menubutton

The event is triggered when the menu button is pressed.

6

searchbutton

The event is triggered when the Android search button is pressed.

7

startcallbutton

The event is triggered when the start call button is pressed.

8

endcallbutton

The event is triggered when the end call button is pressed.

9

volumedownbutton

The event is triggered when the volume down button is pressed.

10

volumeupbutton

The event is triggered when the volume up button is pressed.

Use events

All events are used in a similar manner. /b10> We should always add an event listener to js instead of an inline event call, because The Cordova content security policy does not allow built-in Javascript. /b11> If we try to call the event inline, we get the following error.

Cordova event

The correct way to use events is to use addEventListener. /b10> We'll show you an example of using the volumeupbutton event.

document.addEventListener("volumeupbutton", callbackFunction, false);

function callbackFunction() {
   alert('Volume Up Button is pressed!')
}

Once we press the volume up button, the alarm will appear on the screen.

Cordova event

Process the return button

You usually want to use some of the application features of the Android Return button, such as going back to the last screen. /b10> In order to be able to implement your own functionality, you first need to disable exiting the application when you press the return button.

document.addEventListener("backbutton", onBackKeyDown, false);

function onBackKeyDown(e) {
   e.preventDefault();
   alert('Back Button is Pressed!');
}

Now, when we press the home Android back button, the alert appears on the screen instead of exiting the app. /b10> This is done by using e.preventDefault().

Cordova event