muszek muszek - 4 months ago 27x
Android Question

Cordova - detect that app is in the background

I have a feature in my cordova app that lets user do something when user shakes the phone (I'm using shake.js for the purpose). The phone vibrates when it happens.

The problem is that, when I "alt tab" away from the app and it's in the background, the shake/vibrate combo still works. So the user might be using a completely different app and the phone would still vibrate.

Is there a way to detect that the app is in the background, or better yet, freeze the app somehow when it's going to the background?

I have this problem on Android (haven't tested it on iOS yet).


You can simply bind the pause event and you remove the shake feature from your app. According to the documentation The (pause) event fires when an application is put into the background.

You can listen for the resume event to put back the shake feature. As usual in the documentation you can find complete code examples.