Resuming a phonegap app on android and/or iOS

edited January 2012 in Packaging Apps
On webOS we are used to having control over when an application is closed. On android or iOS it is managed by the OS, not the user. This means that if a user switches away from your application and then back that your app may be relaunched. In some cases this isn't a big problem, but it may mean that whatever the user was doing is lost.

You can work around this by saving the state when the app exits and then restoring that state at startup.

Phonegap sends an event called "pause" when the user navigates away from the application. When this event fires write your current state to local storage. Register for the event in the create function of your enyo kind.
   document.addEventListener("pause"), enyo.bind(this, this.paused), false);
and add a function like this to your kind:
   paused: function()
      window.localStorage.setItem("laststate", enyo.json.stringify(this.getState());
Your create function can load the state and do any required setup to restore that state:
   var statejson = window.localStorage.getItem("laststate");

   if (statejson) {
      try {
         var state = enyo.json.parse(statejson);
      } catch (e) {
         // The JSON could not be parsed...
This still requires a lot of effort to make sure that you can save your state, and restore it. This method also means that if the format of your state object changes then you will have to deal with old versions if your app is updated. A simple way to deal with that is to include a version number in your state object and discard it if the version does not match.


  • PhoneGap also provides an event called “resume” which is fired if the app is resumed but not at the first start. That's useful if you only want to reload the data as long as the app is not completely closed.
Sign In or Register to comment.