# How to detect when the user leaves the app (temporarily)?

**URL:** <https://forum.ionicframework.com/t/how-to-detect-when-the-user-leaves-the-app-temporarily/20884>\
**Category:** ionic-v1\
**Created:** [April 13, 2015, 4:06pm UTC](https://forum.ionicframework.com/t/how-to-detect-when-the-user-leaves-the-app-temporarily/20884 "2015-04-13T16:06:02Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![sibizavic1](https://avatars.discourse-cdn.com/v4/letter/s/f475e1/32.png) [@sibizavic1](https://forum.ionicframework.com/u/sibizavic1)\
**Post date:** [April 13, 2015, 4:06pm UTC](https://forum.ionicframework.com/t/how-to-detect-when-the-user-leaves-the-app-temporarily/20884/1 "2015-04-13T16:06:03Z")

</div>

Is there a way using javascript/cordova to detect when the user has pressed the home button, and is temporarily out of the app (pause mode).

In the Ionic framework, you have the `ionicview.leave`, but this also holds when the user leaves the view and goes to another one.

_Context:_  
I am building an app that is protected, meaning that you can only view the content when:

- you enter the correct access code
- your session has not timed out

But when you go to Home, then I want somehow to record that the session is over and that the user needs to authenticate when he comes back in the app.

---

<div class="post-metadata">

**Author:** ![yurinondual](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/yurinondual/32/92490_2.png) [@yurinondual](https://forum.ionicframework.com/u/yurinondual)\
**Post date:** [April 13, 2015, 4:19pm UTC](https://forum.ionicframework.com/t/how-to-detect-when-the-user-leaves-the-app-temporarily/20884/2 "2015-04-13T16:19:48Z")

</div>

I am curious if browser Page Visibility API will work in this case?

---

<div class="post-metadata">

**Author:** ![yurinondual](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/yurinondual/32/92490_2.png) [@yurinondual](https://forum.ionicframework.com/u/yurinondual)\
**Post date:** [April 13, 2015, 4:22pm UTC](https://forum.ionicframework.com/t/how-to-detect-when-the-user-leaves-the-app-temporarily/20884/3 "2015-04-13T16:22:21Z")

</div>

You can also use these events;

```
$document.addEventListener('resume', function() {
    $rootScope.$broadcast('onResume');
  });
  $document.addEventListener('pause', function() {
    $rootScope.$broadcast('onPause');
  });

```

> **[Best way to intercept events like cordova resume and pause in ionic](http://forum.ionicframework.com/t/best-way-to-intercept-events-like-cordova-resume-and-pause-in-ionic/4720)**
>
> Hi, I need to intercept events like pause (in order to save application data to local storage) and restore it (on resume events) in order to keep my application running as sometimes the application restarts without any reason... What will be the...

---

<div class="post-metadata">

**Author:** ![brandyshea](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/brandyshea/32/148286_2.png) [@brandyshea](https://forum.ionicframework.com/u/brandyshea)\
**Post date:** [April 13, 2015, 4:44pm UTC](https://forum.ionicframework.com/t/how-to-detect-when-the-user-leaves-the-app-temporarily/20884/4 "2015-04-13T16:44:14Z")

</div>

Just to add on to @yurinondual you can use the [$ionicPlatform](http://ionicframework.com/docs/api/service/%24ionicPlatform/) events to add the cordova event listeners:

```
$ionicPlatform.on('resume', function() {
    $rootScope.$broadcast('onResume');
});

$ionicPlatform.on('pause', function() {
    $rootScope.$broadcast('onPause');
});

```

---

<div class="post-metadata">

**Author:** ![sibizavic1](https://avatars.discourse-cdn.com/v4/letter/s/f475e1/32.png) [@sibizavic1](https://forum.ionicframework.com/u/sibizavic1)\
**Post date:** [April 14, 2015, 12:18pm UTC](https://forum.ionicframework.com/t/how-to-detect-when-the-user-leaves-the-app-temporarily/20884/5 "2015-04-14T12:18:19Z")

</div>

Thanks for the comments everyone. Just where do I put these event listeners?

---

<div class="post-metadata">

**Author:** ![brandyshea](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/brandyshea/32/148286_2.png) [@brandyshea](https://forum.ionicframework.com/u/brandyshea)\
**Post date:** [April 14, 2015, 2:29pm UTC](https://forum.ionicframework.com/t/how-to-detect-when-the-user-leaves-the-app-temporarily/20884/6 "2015-04-14T14:29:26Z")

</div>

I put them in the app’s run function:

```
app.run(function($ionicPlatform) {
    $ionicPlatform.on('resume', function() {
        // code for resuming the app
    });
});
```

---

<div class="post-metadata">

**Author:** ![prashantbabber2812](https://avatars.discourse-cdn.com/v4/letter/p/bbe5ce/32.png) [@prashantbabber2812](https://forum.ionicframework.com/u/prashantbabber2812)\
**Post date:** [August 13, 2020, 11:36pm UTC](https://forum.ionicframework.com/t/how-to-detect-when-the-user-leaves-the-app-temporarily/20884/7 "2020-08-13T23:36:50Z")

</div>

Hello,

I have biometric authentication enabled in my ionic cordova android app. I wish to call the biometric authentication everytime user pause and then resumes the app. but I am facing a small issue, the app is considering the biometric auth as a pause event and it is asking to reauth even when I am routing from Login to Home page

---

<div class="post-metadata">

**Author:** ![indraraj26](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/indraraj26/32/131213_2.png) [@indraraj26](https://forum.ionicframework.com/u/indraraj26)\
**Post date:** [August 16, 2020, 1:49am UTC](https://forum.ionicframework.com/t/how-to-detect-when-the-user-leaves-the-app-temporarily/20884/8 "2020-08-16T01:49:45Z")

</div>

are you using resume and pause event from platform?

---

<div class="post-metadata">

**Author:** ![mhartington](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/mhartington/32/137757_2.png) [@mhartington](https://forum.ionicframework.com/u/mhartington)\
**Post date:** [July 19, 2021, 8:54pm UTC](https://forum.ionicframework.com/t/how-to-detect-when-the-user-leaves-the-app-temporarily/20884/9 "2021-07-19T20:54:15Z")

</div>

Closed on July 19, 2021, 8:54pm UTC


