In the series of learning, building apps using Apache Cordova, this article will explain you which events you can use in Cordova and how to use them.

There are numerous events that can be used in Cordova mobile application projects.

The events available are listed in the following table.

Apache Cordova Events

SN

Events & Details

1

deviceReady

This event is invoked when the Cordova is fully loaded. This ensures that no other Cordova functions are called before everything is loaded.

2

pause

This event is triggered when the app is made run into background.

3

resume

This event is triggered when the app is resumed from background running.

4

backbutton

This event is triggered when the back button is pressed.

5

menubutton

This event is triggered when 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 in the same manner. you should always add event listeners in our js file in place of inline event calling since Cordova Content Security Policy doesn't allow inline Javascript.

The correct way of handling with events is by using addEventListener. We will show you example of using volumeupbutton event.

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

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

 When you press the volume up button, You will see the following on the screen.

Handling Events in App Built Using Apache Cordova

Handling Back Button

You generally want to use back button in your app for some app functionality like returning to previous screen. To be able to implement your own functionality, you first need to disable exiting the app when the back button is pressed.    

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 message will appear on screen in place of exiting the app. This is done by using a finction called e.preventDefault().

Handling Events in App Built Using Apache Cordova


Read Our Previous Posts:Introduction to Apache Cordova

                                                                      How to Install Apache Cordova

                                                                      Create First App Using Apache Cordova

                                                                       Apache Cordova- Storing Data Through Apps

  Modified On Mar-19-2018 01:14:46 AM

Leave Comment