Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not all View life cycle events firing for tabs #2869

Closed
mhartington opened this issue Jan 5, 2015 · 28 comments
Closed

Not all View life cycle events firing for tabs #2869

mhartington opened this issue Jan 5, 2015 · 28 comments
Assignees
Milestone

Comments

@mhartington
Copy link
Member

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.

@ajoslin ajoslin changed the title Not all View life cycle events not firing for tabs Not all View life cycle events firing for tabs Jan 5, 2015
@jameslegue
Copy link

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

@kepo80
Copy link

kepo80 commented Feb 8, 2015

+1

@adamdbradley adamdbradley self-assigned this Feb 9, 2015
@adamdbradley adamdbradley added this to the 1.0.0-rc0 milestone Feb 9, 2015
@ag-secret
Copy link

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

@madgrizzle
Copy link

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

@jianbo
Copy link

jianbo commented Mar 4, 2015

Still having the problem nightly

@jameslegue
Copy link

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

@madgrizzle
Copy link

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)

@Alkashi
Copy link

Alkashi commented Mar 16, 2015

Issue still not fixed for me...

@mhartington
Copy link
Member Author

Seeing this as well

cc @adamdbradley

@mhartington mhartington reopened this Mar 16, 2015
@mica16
Copy link

mica16 commented Mar 16, 2015

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 ;)

@mica16
Copy link

mica16 commented Mar 16, 2015

@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 ;)

@mica16
Copy link

mica16 commented Mar 17, 2015

@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 !

@perrygovier perrygovier modified the milestones: 1.0.0-rc2, 1.0.0-rc1 Mar 20, 2015
@cranesandcaff
Copy link

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.

@Fayozjon
Copy link

Patrick

http://thefitfactory.net/volatilnost

23.03.2015, 17:54, "Patrick Cauley" notifications@github.com: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.

—Reply to this email directly or .

@adamdbradley
Copy link
Contributor

@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.

@adamdbradley adamdbradley added the needs: reply the issue needs a response from the user label Mar 24, 2015
@Fayozjon
Copy link

mica16

http://sustainabletechknow.com/forum_webmoney

17.03.2015, 16:13, "mica16" notifications@github.com: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 !

—Reply to this email directly or .

@perrygovier perrygovier modified the milestones: 1.0.0-rc2, 1.0.0-rc3 Mar 30, 2015
@adamdbradley adamdbradley removed the needs: reply the issue needs a response from the user label Apr 2, 2015
@ioudas210
Copy link

I have this issue in ionic 1.0.0

@mhartington
Copy link
Member Author

Could you produce an example of this?

@pluswave
Copy link

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

@mhartington
Copy link
Member Author

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.

@y-x-c
Copy link

y-x-c commented Aug 11, 2015

@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?

@mhartington
Copy link
Member Author

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

@eternalsayed
Copy link

@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

@bitcoinvsalts
Copy link

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

@devilankur18
Copy link

+1 Doesn't work when cache = false

@SeanHub
Copy link

SeanHub commented Oct 1, 2016

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...

@Herb-sh
Copy link

Herb-sh commented Mar 20, 2017

I found a work around to this, have a look http://stackoverflow.com/questions/28938408/ionic-view-leave-events-never-called/42905435#42905435.

Hope it helps!

@ionitron-bot
Copy link

ionitron-bot bot commented Sep 3, 2018

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.

@ionitron-bot ionitron-bot bot locked and limited conversation to collaborators Sep 3, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests