Ionic-framework: Not all View life cycle events firing for tabs

Created on 5 Jan 2015  路  28Comments  路  Source: ionic-team/ionic-framework

When changing tabs, certain view life-cycle events do not get executed.

http://codepen.io/mhartington/pen/qEqyBR

If you switch to the about tab, you'll get an alert from the beforeEnter event, but no alert form the beforeLeave event.

Most helpful comment

Was this problem ever fixed? Its happening for me on 1.3.1

For those getting annoyed, switching back to 1.3.0 seems to work. I didn't know a patch update was supposed to introduce breaking changes...

All 28 comments

+1 on this bug - would appreciate a fix or potential work around if available.

+1

+1
$ionicView unload, beforeLeave, afterLeave and leave don't fire on tab change view.

This fix doesn't seem to work for me. I created a new app using the tabs starter and under the 'FriendsCtrl' I added a series of $scope.$on('$ionicView.enter', ....) functions that logs enter, leave, beforeLeave, afterLeave for both $ionicView and $ionicNavView. Besides using the latest nightly, this is the only change I made to the template (I'll mention I also use Crosswalk).

When I click the friends tab, I get events firing as follows from the code in the FriendCtrlr:
$ionicNavView.beforeLeave
$ionicView.enter
$ionicNavView.afterLeave
$ionicNavView.leave

The NavView leaves fire inside the FriendCtrlr, not the previous controller.

I looked at the code referenced in the fix and the change to ViewSwitcher.js seems problematic. Within the emit function, scope is immediately assigned to the 'enteringEle.scope'. The code that was added to the function uses that same scope to emit the NavView leaves... thereby causing the NavView leaves to be emitted in the wrong controller?

Here's a codepen with what I'm talking about...

http://codepen.io/anon/pen/azGGYq?editors=001

Still having the problem nightly

Agree - problem still persists and @madgrizzle made a good explanation of what the root cause appears to be.

This fix fires the Leave events in the leaving controller but still fires the NavLeave events in the arriving controller (still that issue with the ViewSwitcher.js change in 9cc61ec)

Issue still not fixed for me...

Seeing this as well

cc @adamdbradley

Wow, I was very very anxious about this bug I just noticed this morning.
It causes memory leaks since resources were initialized and not closed each time view is generated.
I was using the beforeLeave event to close those resources (like a subscriber of notifications and some $timeouts).
I've just updated to rc1, but it doesn't seem to fix it.

Basically I have a view on Tab A.
Its controller declares this:

$scope.$on('$ionicView.beforeLeave', function () {
  alert("life cycle Ok");
});

When I simply go back (in the same tab, assuming I was forward in the history), it works, the alert is shown.

However, if I DIRECTLY click on tab B, the callback is not called: no alert.

Any idea @mhartington @adamdbradley ?
I can't release my app after seeing this kind of bug ;) It causes too many side effects.

Thanks ;)

@adamdbradley @mhartington , I've found the use case that reproduces the bug.
Regarding this http://codepen.io/mica16/pen/jEQGvO.

Working scenario:
From the first view of the first tab, just click on the "About" tab.
BeforeEnter and BeforeLeave event are played.
Good !

Buggy scenario:
From the SECOND view of the first tab (meaning forward in the history), just click on the "About" tab.
Only BeforeEnter is played ! No BeforeLeave....
Bad :(

That's the whole of my issue since I need to trigger this leave event from a deep view (meaning forward in the tab's views' history) .

Thus the key point is the tab's "history".

A quick fix in the nightly (if possible) would be a gift ;)

@adamdbradley @mhartington Sorry for all those (my) posts, but I found the real reason:
My current view was not cached (cache: false), explaining why beforeLeave was not triggered when changing tab.
Only when you go back or go forward in the same tab, it is triggered.

So I have to deal with $scope.$on('$destroy') in order to make the job ;), typically called when the view is not cached.

So in the official Ionic doc:
The view has finished leaving and is no longer the active view. This event will fire, whether it is cached or destroyed. => this is true ONLY if you don't switch tab directly from a view that is NOT cached.

It may help some developers, by checking whether their views are cached before dealing with the beforeLeave event while switching tabs.

Thanks !

I have a similar possibly the same issue, All of the beforeEnter, enter, and afterEnter events fire however none of the beforeLeave, leave, or afterLeave events fire. However the $destory events do fire.

Patrick

http://thefitfactory.net/volatilnost

23.03.2015, 17:54, "Patrick Cauley" [email protected]:I have a similar possibly the same issue, All of the beforeEnter, enter, and afterEnter events fire however none of the beforeLeave, leave, or afterLeave events fire. However the $destory events do fire.

鈥擱eply to this email directly or .

@mica16 In your demo, the HomeCtrl is only assigned to the home view, that happens to be in the tab that's labeled home. The tabs.facts2 route doesn't have a controller, so it's never creates a beforeLeave event.

mica16

http://sustainabletechknow.com/forum_webmoney

17.03.2015, 16:13, "mica16" [email protected]:Sorry for all those (my) posts, but I found the real reason:
my current view was not cached (cache: false), explaining why beforeLeave was not triggered when changing tab.
Only when you go back or go forward in the same tab, it is triggered.

So I have to deal with $scope.$on('$destroy') in order to make the job ;)

Therefore, it's not a bug.

Thanks !

鈥擱eply to this email directly or .

I have this issue in ionic 1.0.0

Could you produce an example of this?

I think I just faced the same issue:

for leaving the whole tabs scope, the "beforeLeave" event is fired at the scope associated with the tabs, but not a detailed tab.

Just tested on ionic lib 1.0.1

Just want to stress that everything in the view events is async, so they're really tough to pin point when exactly they happen. So even if they're being reported back as happening after another event, they really aren't.

@mhartington I have a similar issue in nested view. Say a.b is abstract view, if I transition from state a.b.c to state a, '$ionic.beforeLeave' will only be fired on a.b state but not a.b.c. Is this a expected behavior?

@Orthocenter try using $ionicNavView when dealing with abstract states.

@mhartington: Tried your suggestion but that doesn't work. Basically, I've four tabs. I want to show a loader whenever tab is changed and hide it when the tab is loaded. While $ionicView.enter is working fine, $ionicView.beforeLeave isn't working at all. Any suggestions? Thanks

try enabling the cache view, it will work. it does not work when cache = false

+1 Doesn't work when cache = false

Was this problem ever fixed? Its happening for me on 1.3.1

For those getting annoyed, switching back to 1.3.0 seems to work. I didn't know a patch update was supposed to introduce breaking changes...

Thanks for the issue! This issue is being locked to prevent comments that are not relevant to the original issue. If this is still an issue with the latest version of Ionic, please create a new issue and ensure the template is fully filled out.

Was this page helpful?
0 / 5 - 0 ratings