App event for when in foreground and background


I need to fire some functions when the application is coming from background to foreground. I have read a question that asks something similar. I am using Cordova though.
I am not sure where to add the EventListeners.

Any help appreciated.

Cordova fires two events to handle this: “pause” and “resume”. You can add event handlers for this, but be aware that you should only do very short actions in the “pause” event, as iOS/Android will suspend your app process in a few seconds. There are API’s at least for iOS, which allow some further background processing but I’ve never used them and don’t know if there is a Cordova plugin for support.

Events are described with examples on this page:


Im using this to refresh/reload my start page, when my app comes from background to foreground.

document.addEventListener("visibilitychange", function() {
  app.emit('visibilitychange', document.visibilityState)

app.on('visibilitychange', function(state) {
  if (state === 'visible'){
	  //app.dialog.alert("app visible state",state)
	  app.views.mainView.router.refreshPage({ignoreCache: true, reloadCurrent:true, force:true});

But I´m not using Cordova, but as Tim says, in Cordova I use this. And if I remember correctly you need the settimeout for it to work!?

document.addEventListener("resume", onResume, false);
document.addEventListener("pause", onPause, false);

function onResume(){
	setTimeout(function () {
	}, 1000);

function onPause(){
	setTimeout(function () {
	//do something here...
	}, 1000);

Thank you very much. Actually what I need to do is when the app resumes, so the time does not matter that much.

I think you need the settimout even if you set it to 0.

1 Like