Cordova - Events




Cordova - Events

There are various events that can be used in Cordova projects. The following table shows the available events.

S.No Events & Details
1

deviceReady

This event is triggered once Cordova is fully loaded. This helps to ensure that no Cordova functions are called before everything is loaded.

2

pause

This event is triggered when the app is put into background.

3

resume

This event is triggered when the app is returned from background.

4

backbutton

This event is triggered when the back button is pressed.

5

menubutton

This event is triggered when the menu button is pressed.

6

searchbutton

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

7

startcallbutton

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

8

endcallbutton

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

9

volumedownbutton

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

10

volumeupbutton

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

Using Events

All of the events are used almost the same way. We should always add event listeners in our js instead of the inline event calling since the Cordova Content Security Policy doesn't allow inline Javascript. If we try to call event inline, the following error will be displayed.

Cordova - Events

The right way of working with events is by using addEventListener. We will understand how to use the volumeupbutton event through an example.

document.addEventListener("volumeupbutton", callbackFunction, false);  
function callbackFunction() { 
   alert('Volume Up Button is pressed!');
}

Once we press the volume up button, the screen will display the following alert.

Cordova - Events

Handling Back Button

We should use the Android back button for app functionalities like returning to the previous screen. To implement your own functionality, we should first disable the back button that is used to exit the App.

document.addEventListener("backbutton", onBackKeyDown, false);  
function onBackKeyDown(e) { 
   e.preventDefault(); 
   alert('Back Button is Pressed!'); 
} 

Now when we press the native Android back button, the alert will appear on the screen instead of exiting the app. This is done by using the e.preventDefault() command.

Cordova - Events



Frequently Asked Questions

+
Ans: Cordova - Storage view more..
+
Ans: Cordova - config.xml File view more..
+
Ans: Cordova - First Application view more..
+
Ans: Cordova - Events view more..
+
Ans: Cordova - Back Button view more..
+
Ans: Cordova - Plugman view more..
+
Ans: Cordova - Battery Status view more..
+
Ans: Cordova - Camera view more..
+
Ans: Cordova - Contacts view more..
+
Ans: Cordova - Device view more..
+
Ans: Cordova - Accelerometer view more..
+
Ans: Cordova - Device Orientation view more..
+
Ans: Cordova - Dialogs view more..
+
Ans: Cordova - File System view more..
+
Ans: Cordova - File Transfer view more..
+
Ans: Cordova - Geolocation view more..
+
Ans: Cordova - Globalization view more..
+
Ans: Cordova - InAppBrowser view more..




Rating - NAN/5
515 views

Advertisements