Ionic app did become active

How do I know when the app has become active? That is, when the user opens the app for the first time OR when they switch from one app this this app? Also, how do I know when they closed my app?

Reason is because if they are out for a minute I need to log them out otherwise I need to keep them logged in. What are the functions in ionic that I’m looking for?

You can use the ready function for the first time the app is opened:

$ionicPlatform.ready(function() {
  // app is ready
});

Then you can use cordova events with $ionicPlatform for watching when an app is paused/resumed:

$ionicPlatform.on("pause", function(event) {
    // user put the app in the background
});

$ionicPlatform.on("resume", function(event) {
    // user opened the app from the background
});
6 Likes

@brandyshea In a tabbed app, would you need to put the cordova events in each tab’s controller, or some single place?

Hi,

i have been tested on ionic3, no need to place the code into dedicated tab controller, just place the code in the app.component.ts. see demo:

this.platform.resume.subscribe((res) => {
            console.log('resumed');
        });