ionic app resume and pause

user1242574 picture user1242574 · May 15, 2015 · Viewed 12.6k times · Source

I want my app to do something when it comes form it's background state to the active state. I understand according the cordova documentation I can do this with the code below and this is working.

// device APIs are available
//
function onDeviceReady() {
   document.addEventListener("resume", onResume, false);
}

// Handle the resume event
//
   function onResume() {
}

My app is build with Ionic and the downside of using the code above is that it only work outside my app module, so I can't trigger functions inside my app module. I found a few code example on how it should work inside my app module, but non of them are working. See some examples below.

$ionicPlatform.on('resume', function(){
    // rock on
});

/

ionic.Platform.ready(function() {
    ionic.on('resume', function(){
        //rock on
    }, element);
});

/

$ionicPlatform.ready(function () {
        document.addEventListener("deviceReady", function () {
            document.addEventListener("resume", function () {
                $timeout(function () {
                    //rock on
                }, 0);
            }, false);
        });
    });

Am I doing something wrong am I forgetting something, I hope someone can help me with this.
Thanks!!

Answer

Santosh Shinde picture Santosh Shinde · Aug 27, 2015

For the ionic you try the following code, hopes it helps to you

.run(function($ionicPlatform) {
     $ionicPlatform.ready(function() {
      document.addEventListener("pause", function() {
       //code for action on pause
      }, false);
      document.addEventListener("resume", function() {
       //code for action on resume
      }, false);
});