# Best way to intercept events like cordova resume and pause in ionic

**URL:** <https://forum.ionicframework.com/t/best-way-to-intercept-events-like-cordova-resume-and-pause-in-ionic/4720>\
**Category:** ionic-v1\
**Created:** [May 24, 2014, 7:21am UTC](https://forum.ionicframework.com/t/best-way-to-intercept-events-like-cordova-resume-and-pause-in-ionic/4720 "2014-05-24T07:21:51Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![glazilla](https://avatars.discourse-cdn.com/v4/letter/g/54ee81/32.png) [@glazilla](https://forum.ionicframework.com/u/glazilla)\
**Post date:** [May 24, 2014, 7:21am UTC](https://forum.ionicframework.com/t/best-way-to-intercept-events-like-cordova-resume-and-pause-in-ionic/4720/1 "2014-05-24T07:21:52Z")

</div>

Hi,

I need to intercept events like pause (in order to save application data to local storage) and restore it (on resume events) in order to keep my application running as sometimes the application restarts without any reason…  
What will be the best way to do that and where can I put the appropriate code ?  
Regards,  
Sylvain

---

<div class="post-metadata">

**Author:** ![Calendee](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/calendee/32/13656_2.png) [@Calendee](https://forum.ionicframework.com/u/Calendee)\
**Post date:** [May 27, 2014, 12:09pm UTC](https://forum.ionicframework.com/t/best-way-to-intercept-events-like-cordova-resume-and-pause-in-ionic/4720/2 "2014-05-27T12:09:49Z")

</div>

I don’t know if this is the “best” place. I have a “main” controller that resides on the `<body>`. In that controller, I simply listen for the document events.

```auto
    document.addEventListener("resume", function() {

        if($state.current.name.indexOf('main') !== -1 ) {

            $state.go('main.home');

            $scope.appCtrl.resetMember();

            // Close the loading indicator just in case it's still showing
            $scope.hideLoadingIndicator();

        }

    }, false);

    document.addEventListener("pause", function() {

        if($state.current.name.indexOf('main') !== -1 ) {

            $state.go('main.home');

            $scope.appCtrl.resetMember();

            // Close the loading indicator just in case it's still showing
            $scope.hideLoadingIndicator();

        }

    }, false);

```

---

<div class="post-metadata">

**Author:** ![francoaa](https://avatars.discourse-cdn.com/v4/letter/f/22d042/32.png) [@francoaa](https://forum.ionicframework.com/u/francoaa)\
**Post date:** [May 27, 2014, 2:25pm UTC](https://forum.ionicframework.com/t/best-way-to-intercept-events-like-cordova-resume-and-pause-in-ionic/4720/3 "2014-05-27T14:25:51Z")

</div>

I think that the best way to doit is creating a service that broadcast the resume, pause, online, etc events to their listeners.

```
App.factory('Device', ['$document', '$rootScope', function($document, $rootScope) {
  ...some other stuff...
  $document.addEventListener('resume', function() {
    $rootScope.$broadcast('onResume');
  });
  $document.addEventListener('pause', function() {
    $rootScope.$broadcast('onPause');
  });
...etc
}]);

```

and on your controller listen to the events with $scope.$on(‘onResume’, function() { do something });  
always remember to remove those listener when you leave the controller!

BTW I didn’t test this code 😃

---

<div class="post-metadata">

**Author:** ![keithdmoore](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/keithdmoore/32/64408_2.png) [@keithdmoore](https://forum.ionicframework.com/u/keithdmoore)\
**Post date:** [May 27, 2014, 2:39pm UTC](https://forum.ionicframework.com/t/best-way-to-intercept-events-like-cordova-resume-and-pause-in-ionic/4720/4 "2014-05-27T14:39:59Z")

</div>

I put these types of listeners in app.js in a run block. If I want to do something elsewhere with them, then I broadcast an event as shown above.

---

<div class="post-metadata">

**Author:** ![akhil](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/akhil/32/8036_2.png) [@akhil](https://forum.ionicframework.com/u/akhil)\
**Post date:** [August 12, 2014, 3:12am UTC](https://forum.ionicframework.com/t/best-way-to-intercept-events-like-cordova-resume-and-pause-in-ionic/4720/5 "2014-08-12T03:12:56Z")

</div>

I have tried the below code, but pause and resume function executes only once when app loads. However when app pauses or restarts I do see log like ‘Handle the pause’ and ‘Resuming the App’ but processPause() and processResume() never executes.

Please help!!!

```
 .run(function($ionicPlatform, $rootScope, SQLService) {
    	$ionicPlatform.ready(function() {	
    		document.addEventListener("deviceready", onDeviceReady, false);
    	});
    	
    	function onDeviceReady(){
    		//Add Pause event listener for autolock
    		window.addEventListener("pause", SQLService.processPause() , false);
    
    		//Add Resume event listener for autolock
    		window.addEventListener("resume", SQLService.processResume(), false);
    		
    		SQLService.startApp();
    	};
    })
```

---

<div class="post-metadata">

**Author:** ![zeronil](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/zeronil/32/91071_2.png) [@zeronil](https://forum.ionicframework.com/u/zeronil)\
**Post date:** [January 8, 2015, 10:56pm UTC](https://forum.ionicframework.com/t/best-way-to-intercept-events-like-cordova-resume-and-pause-in-ionic/4720/6 "2015-01-08T22:56:14Z")

</div>

I’ve been able to get this to work, but code that executes during a pause does not seem to be able to do much (like save data via $http):

> .run(function ($ionicPlatform, $rootScope) {

> ```
> $ionicPlatform.ready(function () {
> document.addEventListener("deviceready", onDeviceReady, false);
> });
> 
> ```

> ```
> function onDeviceReady() {
> 
> ```

> ```
> console.log('run() -> onDeviceReady');
> 
> ```

> ```
> document.addEventListener("pause", function (event) {
> $rootScope.$broadcast('cordovaPauseEvent');
> console.log('run() -> cordovaPauseEvent');
> });
> 
> ```

> ```
> document.addEventListener("resume", function (event) {
> $rootScope.$broadcast('cordovaResumeEvent');
> console.log('run() -> cordovaResumeEvent');
> });
> }
> 
> ```
> 
> }

---

<div class="post-metadata">

**Author:** ![lrolecek](https://avatars.discourse-cdn.com/v4/letter/l/bc79bd/32.png) [@lrolecek](https://forum.ionicframework.com/u/lrolecek)\
**Post date:** [January 12, 2015, 2:59pm UTC](https://forum.ionicframework.com/t/best-way-to-intercept-events-like-cordova-resume-and-pause-in-ionic/4720/7 "2015-01-12T14:59:19Z")

</div>

From Cordova documentation:

> **iOS Quirks**  
> In the pause handler, any calls to the Cordova API or to native plugins that go through Objective-C do not work, along with any interactive calls, such as alerts or console.log(). They are only processed when the app resumes, on the next run loop.

This is the reason why some things won’t work - like using SQLite etc.

---

<div class="post-metadata">

**Author:** ![nonsense](https://avatars.discourse-cdn.com/v4/letter/n/a9a28c/32.png) [@nonsense](https://forum.ionicframework.com/u/nonsense)\
**Post date:** [June 29, 2015, 3:40pm UTC](https://forum.ionicframework.com/t/best-way-to-intercept-events-like-cordova-resume-and-pause-in-ionic/4720/8 "2015-06-29T15:40:20Z")

</div>

So anyone have solution for this guy?

---

<div class="post-metadata">

**Author:** ![treygood](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/treygood/32/90590_2.png) [@treygood](https://forum.ionicframework.com/u/treygood)\
**Post date:** [July 8, 2015, 8:36am UTC](https://forum.ionicframework.com/t/best-way-to-intercept-events-like-cordova-resume-and-pause-in-ionic/4720/9 "2015-07-08T08:36:20Z")

</div>

@francoaa, Yeah, the code doesn’t work 😄

Here is a factory example using corrected syntax. Note the use of JQLite’s _bind_ syntax.

```
angular.module('myApp').factory('broadcast', function ($rootScope, $document) {
    var _events = {
        onPause: 'onPause',
        onResume: 'onResume'
    };
    $document.bind('resume', function () {
        _publish(_events.onResume, null);
    });
    $document.bind('pause', function () {
        _publish(_events.onPause, null);
    });

    function _publish(eventName, data) {
        $rootScope.$broadcast(eventName, data)
    }
    return {
        events: _events
    }
});

```

Then subscribe where needed.

```
$scope.$on(broadcast.events.onPause, function (event) {
    //TODO: Do something
});
$scope.$on(broadcast.events.onResume, function (event) {
    //TODO: Do something
});

```

I prefer the use of _$document_ over simply _document_.

I also use the _events_ object to prevent magic numbers and to prevent fat fingering text constants (intellisense).

---

<div class="post-metadata">

**Author:** ![eno](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/eno/32/64977_2.png) [@eno](https://forum.ionicframework.com/u/eno)\
**Post date:** [July 15, 2015, 9:04pm UTC](https://forum.ionicframework.com/t/best-way-to-intercept-events-like-cordova-resume-and-pause-in-ionic/4720/10 "2015-07-15T21:04:18Z")

</div>

If its just a one time thing in a specific controller you can also do something as simple as this:

```
var destroy = $ionicPlatform.on('resume', function(event) {
  // check event, do some database work, etc.
});

$scope.$on('$destroy', function(event) {
  destroy(); // i.e. removes itself when context destroyed
});
```

---

<div class="post-metadata">

**Author:** ![Balaji\_Gopal](https://avatars.discourse-cdn.com/v4/letter/b/0ea827/32.png) [@Balaji\_Gopal](https://forum.ionicframework.com/u/Balaji_Gopal)\
**Post date:** [February 29, 2016, 7:12am UTC](https://forum.ionicframework.com/t/best-way-to-intercept-events-like-cordova-resume-and-pause-in-ionic/4720/11 "2016-02-29T07:12:08Z")

</div>

The same thing i have implemented in my, I works great in Android.

But in ios the event is not getting fired only how do i get this issue solved.

---

<div class="post-metadata">

**Author:** ![lilbiscuit](https://avatars.discourse-cdn.com/v4/letter/l/f04885/32.png) [@lilbiscuit](https://forum.ionicframework.com/u/lilbiscuit)\
**Post date:** [August 2, 2016, 11:50am UTC](https://forum.ionicframework.com/t/best-way-to-intercept-events-like-cordova-resume-and-pause-in-ionic/4720/12 "2016-08-02T11:50:28Z")

</div>

> [@zeronil](#):
>
> document.addEventListener(“resume”, function (event) {  
> $rootScope.$broadcast(‘cordovaResumeEvent’);  
> console.log(‘run() -\> cordovaResumeEvent’);  
> });  
> }

This is not what I am seeing. With the following code, “pausing…” is indeed shown in the console after the home button is pressed (ios simulator). Alert does not work here, however.

` $ionicPlatform.on("pause", function(event) { console.log("pausing..."); });`
