最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Ionic run function before app closes - Stack Overflow

programmeradmin3浏览0评论

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 pletely, sending it to the background etc.)

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?

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 pletely, sending it to the background etc.)

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?

Share Improve this question asked Jan 27, 2016 at 9:29 user818700user818700
Add a ment  | 

2 Answers 2

Reset to default 5

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

platform.pause.subscribe(e => {
  this.OptionsSave();
});

window.addEventListener('beforeunload', () => {
  this.OptionsSave();
});

You can check this events list from Cordova docs:

https://cordova.apache/docs/en/5.4.0/cordova/events/events.html

In particular Pause event:

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

document.addEventListener("pause", yourCallbackFunction, false);

发布评论

评论列表(0)

  1. 暂无评论