Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ionic app resume and pause

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!!

like image 778
user1242574 Avatar asked May 15 '15 11:05

user1242574


2 Answers

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);
});
like image 169
Santosh Shinde Avatar answered Oct 14 '22 19:10

Santosh Shinde


We can use the $ionicPlatform to listen the resume event of an ionic application. This event fires once the application resumes.

$ionicPlatform.on("resume", function (event) {
    // will execute when device resume.
});

Have a look at CodeExpertz for example

like image 27
Anishnirmal Avatar answered Oct 14 '22 20:10

Anishnirmal