Search code examples
androideventscordovalisteners

Phonegap 3.3 / 3.4 - event firing issue android


When I close (read: send to background) the app, the "pause" isn't called and when I reload the app the "resume" isn't called.

Any tips on getting this to work would be greatly appreciated!

<script type="text/javascript">
    $(document).ready(function() {
    onLoad();
    });      
</script>   
<script type="text/javascript">
    // 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);
        document.addEventListener("pause", onPause, false);
        appStartUp();
    };

    // Handle the resume event
    //
    function onResume() {
        setTimeout(function() {
        alert("onResume");
        }, 0);
    };
    // Handle the pause event
    //
    function onPause() {
        setTimeout(function() {
        alert("onPause");
        }, 0);
    };
</script>

Ok, in addition to this, the app seems to be reloading each time from the start, so the resume and pause don't do anything, as the app is loading again.

I've added android:launchMode="singleTask" to the android manifest (via config.xml). But it still loads again.


Solution

  • This did the trick for me for Jellybean:

    It still isn't working for KitKat...

    <script type="text/javascript">
    
    document.addEventListener("deviceready", onDeviceReady, false);
    document.addEventListener("pause", onPause, false);
    document.addEventListener("resume", onResume, false);
    
    function onDeviceReady() {
    alert("device ready");
    appStartUp();
    };
    
    // Handle the resume event
    //
    function onResume() {
    setTimeout(function() {
    alert("onResume");
    }, 0);
    };
    // Handle the pause event
    //
    function onPause() {
    setTimeout(function() {
    alert("onPause");
    }, 0);
    };   
    </script>