Working with cordova on resume event


#1

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 */ }


Suspend/Resume event of app
#2

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


#3

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);
});
});


#4

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');
            });
        }

#5

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.


#6

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
});

#7

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…


#8

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


#9

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.


#10

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


#11
// 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