ionic-framework: [4.0.0-rc.0] ion-tabs lifecycle events not working when navigating back from another view
Bug Report
Ionic version: [x] 4.x
Current behavior:
I have some items/cards on my tab pages and navigate to other pages when I click on them. When I go back to the tabs pages from those views, my lifecycle events like ionViewDidEnter() do not fire. They fire when I’m changing tabs, but when I am coming from a non-tab view, they never fire.
Expected behavior: For these lifecycle events to fire.
Steps to reproduce: Create a tab project, navigate to another view from a tab page that isn’t another tab, go back to the tab page and see that they have not fired.
Ionic info:
Ionic:
ionic (Ionic CLI) : 4.6.0 (/usr/lib/node_modules/ionic)
Ionic Framework : @ionic/angular 4.0.0-rc.0
@angular-devkit/build-angular : 0.10.7
@angular-devkit/schematics : 7.1.2
@angular/cli : 7.1.2
@ionic/angular-toolkit : 1.2.1
Cordova:
cordova (Cordova CLI) : not installed
Cordova Platforms : not available
Cordova Plugins : not available
System:
NodeJS : v8.14.0 (/usr/bin/node)
npm : 6.4.1
OS : Linux 4.18
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 10
- Comments: 42 (11 by maintainers)
Hi everyone,
I wanted to provide an update regarding the status of this issue. After discussing with the team, we have determined this is not a bug in Ionic Framework; however, we realize there are valid use cases in which developers may want to listen for lifecycle events on an individual tab. Due to this, we have provided a temporary workaround as well as plans for further development.
Why is this not a bug?
When pages transition in Ionic Framework, they fire lifecycle events. For example, going from
/page1to/page2would fireionViewWillLeaveandionViewDidLeaveevents on the Page1 component andionViewWillEnterandionViewDidEnterevents on the Page2 component. The same logic applies when going from/tabs/tab1to/tabs/tab2. In both of these scenarios, we are staying within the same parention-router-outletcontext.The reported issue occurs when navigating between
ion-router-outletcontexts. In this case, we are seeing it when navigating from/tabs/tab1to/page2. When this transition happens, Tab1 remains the active tab, and the entire tabs context transitions away. As a result, lifecycle events will fire on the root TabsPage component, but not on Tab1.For many users, this is unexpected because Tab1 visually appears to transition away. However, under the hood, the entire tabs context transitions away.
What is the workaround?
Luckily, there is an easy to use workaround for developers who wish to listen for lifecycle events on individual tab pages:
tabs.page.html
tabs.page.ts
In this example we do 2 things:
ionTabsDidChange.Future Work
For future major releases of Ionic Framework, we are investigating bringing Ionic Angular routing closer to what Ionic React does. This would involve each Ionic page having a root
ion-pagecomponent, not including the root tabs page. Lifecycle events would be dispatched on these components.With this approach, the router sees a page transitioning in and a page transitioning out but does not care which router outlet each page is part of.
This would be a breaking change in Ionic Angular as the root tab page would no longer fire lifecycle events. Please note that this has not been finalized, and the implementation details may change. We are actively discussing the best way to do this while weighing the pros and cons of the implementation.
Please let me know if there are any questions. For any additional bugs, please open a new issue. Thank you!
Hi there,
Thanks for all the feedback so far! I have pushed a new nightly build that fixes this issue.
npm i @ionic/angular@4.4.1-dev.201905212016.fb63a6fPerfect you solved all of my use cases. cheer
Still not working in 5.0.7.
Hi everyone,
I am working on a fix for this issue and have released a nightly build of Ionic (
npm i @ionic/angular@4.4.1-dev.201905211329.682f163). It would be great if some people could test it out with their use cases and provide some feedback.We appreciate your patience as we work to resolve this issue. Thanks!
NOT WORKING!!!
Hi all,
@liamdebeasi any updates on that topic? It is still present in
5.0.4My current workaround looks like the following:
Create an interface with the hooks you want to be implemented in your child tabs
Listen to tab changes and lifecycle hooks in your parent tab and forward them to your child tabs
Implement the Tab interface into your child tabs
Hi everyone,
This has not been included in the latest release. The PR that’s up currently does not address the root issue (which is that nested router outlets cause problems with lifecycle hooks). I am going to be discussing this issue with the team soon.
Thanks!
The compile warnings are gone now. thumbsup
However the back link issue still persisted.
%3Dbecomes%253D. Some text escape issue maybe? This also messes with going back if I had query parameters:/adventures/adventures?category=PROJECTbecomes/adventures%3Fcategory%3DPROJECTHmm sounds like a weird dev build. I’ll try to push another one out. Thanks! Glad the main issue is resolved at least!
Thank you @liamdebeasi. I will test it later today and post my feedback.
@brandyscarney thank you. There are 10-20 tickets related to this problem already. And since last year. Hope you can fix it soon.
Any update on this??? I have the same issue. When I am coming back to a page with
mainThis.navCtrl.navigateForward('/tabs/tab2?rand='+random);ionViewDidEnter does not fire .
@rikenpatel20 doesn’t work in a tabbed view though 😦