Search code examples
angularjscordovacordova-pluginsngcordova

Apache Cordova - Detect Android Home button press


I have a project which uses angularjs and I use Cordova to build project to android app. In my project, I add ngCordova and I want to use event when client click home button in device.

I readed http://ngcordova.com/docs/plugins/ but I did not find anything I need. Does anybody can help me or have any idea I can use?


Solution

  • There is no event in cordova for Home button press. You can detect application resume and pause events:

     // device APIs are available
    //  be sure to add the listener in the device ready event
    function onDeviceReady() {
        document.addEventListener("pause", onPause, false);
        document.addEventListener("resume", onResume, false);
    }
    
    //runs when the app is on background
    function onPause() {
        // Handle the pause event
    }
    
    //runs when the app resumes
    function onResume() {
        // Handle the resume event
    }
    

    More info can be found on the official cordova documentation