8 min read

(For more resources related to this topic, see here.)

Pausing your application

Although we want our users to spend their time solely on our applications, they will inevitably leave our application to open another one or do something else entirely. We need to be able to detect when a user has left our application but not closed it down entirely.

How to do it…

We can use the PhoneGap API to fire off a particular event when our application is put into the background on the device:

  1. Create the initial HTML layout for the application, and include the reference to the Cordova JavaScript file in the head tag of the document.

  2. Before the closing head tag, create a new script tag block and add the event listener to check when the device is ready and the PhoneGap code is ready to run.

  3. Create the onDeviceReady function, which will run when the event listener is fired. Inside this, we’ll create a new event listener that will check for a pause event, and once received will fire the onPause method.

    function onDeviceReady() { document.addEventListener("pause", onPause, false); }

  4. Let’s create the onPause method. In this example application, we’ll ask the device to notify the user that the application has moved into the background by playing an audio beep. The numeric parameter specifies how many times we want the audio notification to be played — in this case, just once.

    function onPause() { navigator.notification.beep(1); }

    Developing for iOS? There is no native beep API for iOS. The PhoneGap API will play an audio file using the media API, but the developer must provide the file, named beep.wav and under 30 seconds in length, in the /www directory of the application project files. iOS will also ignore the beep count argument and will play the audio once. If developing for Windows 7 mobile, the WP7 Cordova library contains a generic beep audio file that will be used.

  5. When we run the application on the device, if you press the home button or navigate to another application, the device will play the notification audio.

How it works…

To correctly determine the flow of our lifecycle events, we first set up the deviceready event listener to ensure that the native code was properly loaded. At this point, we were then able to set the new event listener for the pause event.

As soon as the user navigated away from our application, the native code would set it into the background processes on the device and fire the pause event, at which point our listener would run the onPause method.

To find out more about the pause event, please refer to the official documentation, available here:

http://docs.phonegap.com/en/2.0.0/cordova_events_events.md.html#pause.

There’s more…

In this recipe we applied the pause event in an incredibly simple manner. There is a possibility your application will want to do something specific other than sending an audio notification when the user pauses your application.

For example, you may want to save and persist any data currently in the view or in memory, such as any draft work (if dealing with form inputs) or saving responses from a remote API call.

We’ll build an example that will persist data in the next recipe, as we’ll be able to quantify its success when we resume the use of the application and bring it back into the foreground.

Resuming your application

Multi-tasking capabilities that are now available on mobile devices specify that the user has the ability to switch from one application to another at any time. We need to handle this possibility and ensure that we can save and restore any processes and data when the user returns to our application.

How to do it…

We can use the PhoneGap API to detect when our application is brought back into the foreground on the device. The following steps will help us to do so:

  1. Create the initial layout for the HTML and include the JavaScript references to the Cordova and the xui.js files. We will also be setting the deviceready listener once the DOM has fully loaded, so let’s apply an onload attribute to the body tag.

    Resuming an application
  2. Create a new script tag block before the closing head tag and add the deviceready event listener within the onLoad method. We’ll also set two global variables, savedTime, and localStorage, the latter of which will reference the localStorage API on the device:

  3. Create the onDeviceReady function, within which we’ll set the two event listeners to check for the pause and resume events, as follows:

    function onDeviceReady() { document.addEventListener("pause", onPause, false); document.addEventListener("resume", onResume, false); }

  4. We can now add the first of the new callback functions for the added listeners. onPause will run when a pause event has been detected. In this method, we’ll create a new date variable holding the current time, and store it into the global savedTime variable we created earlier.

  5. If the user has entered something in to the text input field, we’ll also take the value and set it into the localStorage API, before clearing out the input field.

    function onPause() { savedTime = new Date(); var strInput = x$('#userInput').attr('value'); if(strInput) { localStorage.setItem('saved_input', strInput); x$('#userInput').attr('value', ''); } }

  6. Define the onResume method, which will run when a resume event has been detected. In this function, we’ll save a new date variable and we’ll use it in conjunction with the savedTime variable created in the onPause method to generate the time difference between the two dates. We’ll then create a string message to display the time details to the user.

  7. We’ll then check the localStorage for the existence of an item stored using the key saved_input. If this exists, we’ll extend the message string and append the saved user input value before setting the message into the DOM to display.

    function onResume() { var currentTime = new Date(); var dateDiff = currentTime.getTime() - savedTime.getTime(); var objDiff = new Object(); objDiff.days = Math.floor(dateDiff/1000/60/60/24); dateDiff -= objDiff.days*1000*60*60*24; objDiff.hours = Math.floor(dateDiff/1000/60/60); dateDiff -= objDiff.hours*1000*60*60; objDiff.minutes = Math.floor(dateDiff/1000/60); dateDiff -= objDiff.minutes*1000*60; objDiff.seconds = Math.floor(dateDiff/1000); var strMessage = '

    You are back!

    ' strMessage += '

    You left me in the background for ' strMessage += '' + objDiff.days + ' days, ' strMessage += '' + objDiff.hours + ' hours, ' strMessage += '' + objDiff.minutes + ' minutes, ' strMessage += '' + objDiff.seconds + ' seconds.

    '; if(localStorage.getItem('saved_input')) { strMessage = strMessage + '

    You had typed the following before you left:

    ' strMessage += '"' + localStorage.getItem('saved_input') + '"

    '; } x$('#message').html(strMessage); }
  8. Finally, let’s add the DOM elements to the application. Create a new div element with the id attribute set to message, and an input text element with the id set to userInput.




  9. When we run the application on the device, the initial output would provide the user with an input box to enter text, should they wish to, as shown in the following screenshot:

  10. If we were to pause the application and then resume it after a period of time, the display would then update to look something like the following screenshot:

How it works…

We set up the deviceready event listener after the DOM was fully loaded, which would then run the onDeviceReady function. Within this method we then added two new event listeners to catch the pause and resume events respectively.

When the application is paused and placed into the background processes on the device, we saved the current date and time into a global variable. We also checked for the existence of any user-supplied input and if it was present we saved it using the localStorage capabilities on the device.

When the application was resumed and placed back into the foreground on the device, the onResume method was run, which obtained the time difference between the saved and current datetime values to output to the user. We also retrieved the saved user input from the localStorage if we had set it within the onPause method.

To find out more about the resume event, please refer to the official documentation, available here:

http://docs.phonegap.com/en/2.0.0/cordova_events_events.md.html#resume.


Subscribe to the weekly Packt Hub newsletter

* indicates required

LEAVE A REPLY

Please enter your comment!
Please enter your name here