Working with cordova on resume event

How can i get the $scope or a instance of controller on resume method? It is possible?
I need make a refresh data inside a controller after on resume!

document.addEventListener(“resume”, onResume, false);
function onResume() { /* code */ }

Right now we don’t have a method that deals with this, but it doesn’t mean its not possible. If your willing to write some stuff, you can make a custom wrapper to deal with this event.

This way, things can be scoped correctly.

http://briantford.com/blog/angular-phonegap

You could also add an event listener in app run method and broadcast an event that you can catch in your specific controller

app.module(‘myApp’,[…]).run(function($rootScope){
window.addEventListener(“resume”, function () {
$rootScope.$broadcast(appEvents.resume);
});
});

3 Likes

Thanks for the replies.

My solution was:

        // Resume refresh
        $rootScope.$on('onResumeCordova', function(event) {
            $scope.refreshList();
        });

        // System events
        document.addEventListener("resume", resume, false);

        function resume() {
            var div = document.getElementsByTagName('body')[0];
            var scope = angular.element(div).scope();
            var rootScope = scope.$root;
            rootScope.$apply(function() {
                rootScope.$broadcast('onResumeCordova');
            });
        }
3 Likes

I used document.addEventListener instead of window.addEventListener cause the window.addEventListener doesn’t work 。

Applications typically should use document.addEventListener to attach an event listener once the devicereadyevent fires.

1 Like

There is a very simple way to to this in ionicframework. Simple inject $ionicPlatform and use on method to listen to the default cordova events like resume

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

I tried this:

$ionicPlatform.on("resume", function(event) {
    $log.info('app resume event', event);
});

but I never see any log messages when I switch to other apps and switch back…

3 Likes

@eno do you have the cordova device plugin (org.apache.cordova.device) installed?

Its in my list of plugins and I see it in my platforms folders (i.e. Device.java for Android and CDVDevice.{m,h} for iOS.

Actually, it seems to be working now - go figure :smile:

// example.js file
// Wait for device API libraries to load
//
function onLoad() {
    document.addEventListener("deviceready", onDeviceReady, false);
}

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

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

https://cordova.apache.org/docs/en/latest/cordova/events/events.html