# Detecting when a user has moved off a view?

**URL:** <https://forum.ionicframework.com/t/detecting-when-a-user-has-moved-off-a-view/14789>\
**Category:** ionic-v1\
**Created:** [December 28, 2014, 7:12am UTC](https://forum.ionicframework.com/t/detecting-when-a-user-has-moved-off-a-view/14789 "2014-12-28T07:12:34Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![CyberFerret](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/cyberferret/32/72111_2.png) [@CyberFerret](https://forum.ionicframework.com/u/CyberFerret)\
**Post date:** [December 28, 2014, 7:12am UTC](https://forum.ionicframework.com/t/detecting-when-a-user-has-moved-off-a-view/14789/1 "2014-12-28T07:12:34Z")

</div>

I seem to be having problems with a view in my Ionic project. This particular view uses $cordovaMedia to play a wav file under the user’s control.

Now, when the user move ‘\< Back’ to the previous view or chooses another tab, I want the $cordovaMedia object to stop playing and unload.

I have included the following code on in my controller:

```
  $scope.$on("$destroy"), function() {
    $cordovaMedia.stop(media);
    $cordovaMedia.release(media);
  };

```

However, when I leave the page, the media keeps playing, and I notice in XCode that the memory usage remains high - so I assume the object is not being stopped and destroyed as I want.

I have also specified ‘cache: false’ in the main state setup:

```
  .state('app.player', {
    cache: false,
    url: "/recording/:recordingId",
    views: {
      'menuContent': {
        templateUrl: "templates/player.html",
        controller: 'PlayerCtrl'
      }
    }
  })

```

I have even tried $scope.$on("$locationChangeStart") instead of “$destroy” to see if that would work, but no difference.

I’d appreciate it if anyone could point out where I am going wrong? Thanks.

---

<div class="post-metadata">

**Author:** ![Mik378](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/mik378/32/7777_2.png) [@Mik378](https://forum.ionicframework.com/u/Mik378)\
**Post date:** [December 28, 2014, 9:20am UTC](https://forum.ionicframework.com/t/detecting-when-a-user-has-moved-off-a-view/14789/2 "2014-12-28T09:20:56Z")

</div>

You should use the events like `$ionicView.leave`.  
Here’s a [complete list](http://ionicframework.com/docs/api/directive/ionView/).

---

<div class="post-metadata">

**Author:** ![CyberFerret](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/cyberferret/32/72111_2.png) [@CyberFerret](https://forum.ionicframework.com/u/CyberFerret)\
**Post date:** [December 28, 2014, 12:45pm UTC](https://forum.ionicframework.com/t/detecting-when-a-user-has-moved-off-a-view/14789/3 "2014-12-28T12:45:47Z")

</div>

Hey @Mik378 - Thanks for your quick reply. I just tried using `$ionicView.leave`, `beforeLeave` and `afterLeave`, but it still does not seem to fire. I placed a `console.log()` message in the function just in case the media was being cached separately, but not getting the message in the console, so I suspect that the event is just not being triggered somehow… ??

Seen a few other threads on here with people reporting a problem with $[ionicView.xxx](http://ionicView.xxx) so not sure if it is a bug in the current beta?

EDIT: Just spotted this, lodged about 1/2 hour ago… [https://github.com/driftyco/ionic/issues/2818](https://github.com/driftyco/ionic/issues/2818)

---

<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:** [December 29, 2014, 10:37pm UTC](https://forum.ionicframework.com/t/detecting-when-a-user-has-moved-off-a-view/14789/4 "2014-12-29T22:37:38Z")

</div>

It seems to work fine for me

> **[Tabs And Navigation: Nightly](https://codepen.io/mhartington/pen/emdozV)**
>
> Demo showing how each tab has its own history stack. Navigating between each tab remembers the correct back and forward views that were visited. Forke...

```auto
  $scope.$on('$ionicView.beforeLeave', function(){
    alert("Before Leaving");
  });

  $scope.$on('$ionicView.afterLeave', function(){
    alert("After Leaving");
  });

```

---

<div class="post-metadata">

**Author:** ![mikemintz](https://avatars.discourse-cdn.com/v4/letter/m/7ea924/32.png) [@mikemintz](https://forum.ionicframework.com/u/mikemintz)\
**Post date:** [December 29, 2014, 11:04pm UTC](https://forum.ionicframework.com/t/detecting-when-a-user-has-moved-off-a-view/14789/5 "2014-12-29T23:04:45Z")

</div>

I think $ionicView.beforeLeave and $ionicView.afterLeave are not called when the user chooses another tab. In the codepen @mhartington just posted, I only get the alert when changing views within the home tab, not when switching away from the home tab.

---

<div class="post-metadata">

**Author:** ![CyberFerret](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/cyberferret/32/72111_2.png) [@CyberFerret](https://forum.ionicframework.com/u/CyberFerret)\
**Post date:** [December 29, 2014, 11:06pm UTC](https://forum.ionicframework.com/t/detecting-when-a-user-has-moved-off-a-view/14789/6 "2014-12-29T23:06:42Z")

</div>

Thanks for testing this.

My app uses the ‘sidemenu’ starter template. I have a side menu which call a view with a list, then tapping on a list item calls the subsequent player view which is where I am having the problem. Not sure if the order of the routing is a factor here. When I go ‘Back’ from the player view to the list again, or use the sidemenu to choose another section of the app, the events don’t seem to fire?

---

<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:** [December 30, 2014, 12:10am UTC](https://forum.ionicframework.com/t/detecting-when-a-user-has-moved-off-a-view/14789/7 "2014-12-30T00:10:32Z")

</div>

@mikemintz Yeah I read more on that github issue after I posted, woops 😁

@CyberFerret Any chance you could create a simplified version of this in a codepen?

---

<div class="post-metadata">

**Author:** ![swaggaguru](https://avatars.discourse-cdn.com/v4/letter/s/f1d935/32.png) [@swaggaguru](https://forum.ionicframework.com/u/swaggaguru)\
**Post date:** [December 30, 2014, 9:48pm UTC](https://forum.ionicframework.com/t/detecting-when-a-user-has-moved-off-a-view/14789/8 "2014-12-30T21:48:02Z")

</div>

@mhartington

Here’s a codepen:

> **[Tabs And Navigation: Nightly](https://codepen.io/anon/pen/QwGLZY)**
>
> Demo showing how each tab has its own history stack. Navigating between each tab remembers the correct back and forward views that were visited. Forke...

Get on the home tab and make the sure the Youtube video player is playing and not muted.  
Click on “Scientific Facts”.  
The video should get removed from the DOM but doesn’t.

It finally does when the user navigates back to the page.

---

<div class="post-metadata">

**Author:** ![CyberFerret](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/cyberferret/32/72111_2.png) [@CyberFerret](https://forum.ionicframework.com/u/CyberFerret)\
**Post date:** [January 3, 2015, 5:17am UTC](https://forum.ionicframework.com/t/detecting-when-a-user-has-moved-off-a-view/14789/9 "2015-01-03T05:17:39Z")

</div>

> [@mhartington](#):
>
> $scope.$on(‘$ionicView.beforeLeave’, function(){  
> alert(“Before Leaving”);  
> });

Ok - I solved my issue. Turned out to be a completely stupid rookie mistake on my part… I had coded my block as:

```
$scope.$on('$ionicView.beforeLeave'), function(){
    alert("Before Leaving");
  };

```

Note the ‘)’ after ‘$ionicView.beforeLeave’ and the missing ‘)’ at the end. Ionic accepted this as a valid block even with this syntax error, and the call was never being triggered! Moving the closing bracket to the proper spot fixed this.

---

<div class="post-metadata">

**Author:** ![Mik378](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/mik378/32/7777_2.png) [@Mik378](https://forum.ionicframework.com/u/Mik378)\
**Post date:** [January 3, 2015, 11:23pm UTC](https://forum.ionicframework.com/t/detecting-when-a-user-has-moved-off-a-view/14789/10 "2015-01-03T23:23:10Z")

</div>

I disagree.  
It still applies:

> “I only get the alert when changing views within the home tab, not when switching away from the home tab.”

In my case also, the `beforeLeave` event isn’t triggered when I click directly on another tab.  
However, the `enter` event is well triggered.  
Sounds like a real bug.

---

<div class="post-metadata">

**Author:** ![Mik378](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/mik378/32/7777_2.png) [@Mik378](https://forum.ionicframework.com/u/Mik378)\
**Post date:** [January 4, 2015, 12:13am UTC](https://forum.ionicframework.com/t/detecting-when-a-user-has-moved-off-a-view/14789/11 "2015-01-04T00:13:06Z")

</div>

Typically, it’s very annoying since I can’t drop some event listeners, specific to a tab, just before switching tabs…

---

<div class="post-metadata">

**Author:** ![CyberFerret](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/cyberferret/32/72111_2.png) [@CyberFerret](https://forum.ionicframework.com/u/CyberFerret)\
**Post date:** [January 4, 2015, 12:33am UTC](https://forum.ionicframework.com/t/detecting-when-a-user-has-moved-off-a-view/14789/12 "2015-01-04T00:33:39Z")

</div>

Apologies - I meant that the issue was solved in MY situation. I am using a SideMenu based app, and it works within that context. Haven’t test in a Tab based app, but I take your word that it is broken there, as other have mentioned it as well. Hopefully will be fixed up by Ionic soon…

---

<div class="post-metadata">

**Author:** ![rajatrocks](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/rajatrocks/32/7009_2.png) [@rajatrocks](https://forum.ionicframework.com/u/rajatrocks)\
**Post date:** [January 5, 2015, 1:21am UTC](https://forum.ionicframework.com/t/detecting-when-a-user-has-moved-off-a-view/14789/13 "2015-01-05T01:21:55Z")

</div>

I’m also having this issue. ionicView.leave and beforeLeave events are not being fired when changing views via tabs.

---

<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:** [January 5, 2015, 3:12pm UTC](https://forum.ionicframework.com/t/detecting-when-a-user-has-moved-off-a-view/14789/14 "2015-01-05T15:12:26Z")

</div>

Hey guys, got an issue open for this.

> <https://github.com/ionic-team/ionic/issues/2869>

Feel free to add to the issue as well.
