Ionic run function before app closes

Is there some kind of function that I can call that listens to whether the app is about to exit, or close or go into the background.. Basically any event that means “the user has stopped using the app”?

I’m my app I build up a ‘user log’ that tracks the user as they are navigating through the app. Instead of sending little pieces of data constantly to the server as these events occur, I want to send off the whole batch in one go just before the user stops using the app (again, whether that means closing the app completely, sending it to the background etc.)

  • How to optimize $interval for real time notifications in Angularjs?
  • How to change Angular bootstrap UI popover trigger event in different devices like tab, mobile etc?
  • AngularJs directive: call method from parent scope within template
  • ng-repeat works wrong AngularJS
  • How to click OK in alert box using protractor
  • Angular JS $location.path(…) not firing route controller
  • And lastly, if such a function does indeed exist.. where do you put that function? In your app.js? Or do you have to put that listener in every single controller of your app?

  • “Error: Mismatched anonymous define()” with Karma + RequireJS + Jasmine
  • Does ng-grid support virtual scrolling?
  • HttpPostedFileBase is null when uploading files with Angular
  • How to populate table dynamically based on data
  • Why do the Angular docs recommend against ng-init in most cases?
  • Use of rowspan to group hierarchical data
  • 2 Solutions collect form web for “Ionic run function before app closes”

    You can check this events list from Cordova docs:

    In particular Pause event:

    The event fires when an application is put into the background.

    document.addEventListener(“pause”, yourCallbackFunction, false);

    This code works in ionic2 native app exit and in the browser as well, when the URL reload button is pressed or the browser tab is closed.

    platform.pause.subscribe(e => {
    window.addEventListener('beforeunload', () => {