1 Reply Latest reply on Dec 15, 2017 12:47 PM by andrei_k

    "pause" and "resume" events are not executing on Android

    andrei_k

      I am unable to use "pause" and "resume" events as described here: Events - Apache Cordova

       

      My test article is working as it should on iOS, but not on Android. On iOS, when I leave the app by opening another app, or put my device to sleep, the "pause" event fires and when I come back, the "resume" event fires. None of this happens on Android.

       

      My basic requirement is to execute some javascript on an interval. But, it doesn't make sense for the code to run when the app is not in the foreground. Has anyone else encountered a similar problem, or can suggest a workaround? (If possible, I'd like to avoid building a custom shell for my app.)

       

      The javascript I'm testing with is:

       

      $(document).ready(function() {

        app.init();

      });

       

      var app = {

        init: function() {

          document.addEventListener('deviceready', app.onDeviceReady, false);

        },

       

        onDeviceReady: function() {

          $("#container").append("<h2>onDeviceReady</h2>");

          document.addEventListener("pause", app.onPause, false);

          document.addEventListener("resume", app.onResume, false);

        },

       

        onPause: function() {

          $("#container").append("<h2>onPause</h2>");

        },

       

        onResume: function() {

          $("#container").append("<h2>onResume</h2>");

        }

      };

        • 1. Re: "pause" and "resume" events are not executing on Android
          andrei_k Level 1

          I determined a workaround to this problem. Although "pause" and "resume" are not fired on Android, the "visibilitychange" event is triggered when the app goes into the background/foreground.

           

          You can use:

          document.addEventListener("visibilitychange", myFunction, false);

           

          Inside myFunction function, you can then check the value of document.visibilityState, (it's either "visible" or "hidden") and proceed accordingly.

           

          Note that the "visibilitychange" event does not fire when the app goes in the background/foreground on iOS. So, you need to rely on "pause" and "resume" for iOS and "visibilitychange" for Android.