Cordova: pause event does not fire

For some reason, the pause event doesn't fire when needed.

If I press the home button on my phone, a pause event should be fired here, which should pause the music. But instead, he continues to play.

But when I open the app again, it suddenly pauses and then resumes. So it fires both events when I return to the application.

Why doesn't onPause fire when I leave the app?

I have the following code:

function onLoad() {
    document.addEventListener("deviceready", onDeviceReady, false);

function onDeviceReady() {
    document.addEventListener("resume", onResume, false);
    document.addEventListener("pause", onPause, false);
}

function onPause() {
    PauseMusic();
    alert('paused');
}

function onResume() {
    ResumeMusic();
    alert('resumed');
}

      

+3


source to share


2 answers


I think I had your same problem. I have set the PhoneGap config.xml KeepRunning variable to false. I set it to true and it now receives a pause event when it is supposed to. Here is the config.xml line I'm talking about:

<preference name="KeepRunning" value="true"/>

      



I suppose it makes sense that if the app is not running then the suspend event code will not be fired until the app is started again. I suppose KeepRunning has a higher priority than listening for a pause event. It would be interesting to get a correct explanation of why this is happening at a lower level.

+7


source


Are you testing on iOS? I found this to be "normal" behavior on iOS, and also Phonegap documentation points out :

In the pause handler, any calls to the Cordova API or native plugins that go through Objective-C do not work alongside any interactive calls such as notifications or console.log (). They are only processed when the application is resumed, in the next run cycle.

So, according to the doc, you never see alert('paused)

when you close the app.



However, my observation is that the code is executed while the application is paused, only the console prints are deferred until the next application is restarted.

If your call PauseMusic()

fails, you should probably post more code and see if there is a problem there.

+3


source







All Articles