$ionicView.leave event doesn't work when switching tabs

“$ionicView.leave called” should be shown in the console when leaving the home tab. Or at least it seems like it should. Is this a bug?

I’m using this as a workaround (probably don’t even need the currentUrl variable check):

var currentUrl = $state.current.url;
$scope.$on('$ionicView.enter', function(){
	var stateChangeListener = $scope.$on('$stateChangeSuccess', function(data){
		if(data.url !== currentUrl){
			console.log('leaving view');
			stateChangeListener();
		}
	});
});
1 Like

+1 I also have the same problem here.

Here too. Unfortunately this prevents me from removing modals from the cached views and causes some additional problems.

Same issue here. Any workaround to remove modals or popover from the cached views ?

Similar problem here (that I mentioned in another thread).

Having trouble getting audio objects to close and deallocate from memory because this event is being ignored by the framework. Hope this is resolved soon.

Same here

I’ve raised a Github issue:

Feel free to provide more information on the ticket.

3 Likes

I use this to get around this problem:

var TIMER = $interval(function() {
    // If we're no longer on the page, cancel the TIMER.
    if( $ionicHistory.currentView().stateName != 'STATE' ){
        $interval.cancel(TIMER);
        return;
    }
    ...
}

The uiRouter has also state-events like:
$stateChangeStart or $stateChangeSuccess ;), where you can check a state-change or if the $stateChange is finished.

Using $stateChangeStart and $stateChangeSuccess also doesn’t fire when changing through tabs.

Hmm, I had success with using $stateChangeStart:

$scope.$on('$ionicView.beforeEnter', function (e) {
  $rootScope.tabClass = 'no-top-border';
});

$scope.$on('$stateChangeStart', function (e) {
  $rootScope.tabClass = '';
});
1 Like

I am having the same issue. $stateChangeStart does not fire, and $ionicView.leave does not fire when changing tabs. I am using ionic v1.3.1. I cant believe its been over a year and Ionic hasn’t fixed this.