Ionic Page LifeCycle Events

Table of contents
Reading Time: 3 minutes
headerIonic1

As we know Ionic pages are nothing but internally it is converted into an Angular component only as we all know Ionic frame is built on the top of Angular. And just like Angular Framework has its lifecycle hooks which we follow during the development of Web Application, Similarly, we have some interesting lifecycle events for ionic framework too which can capture and perform some specific task on a page during its lifecycle.

So In this blog, we are going to see how the page life cycle works in an app built with Ionic and Angular.

ioniclifecycle

The Angular framework provides 2 the most important and commonly used lifecycle events to an ionic framework which we can leverage throughout the life of an application.

1: ngOnit(): This lifecycle event will be fired once after the component initialization. The basic use of this event is the initialization of local variables or if we want to execute any method from the service to get the data from backend to be displayed on UI.

2: ngOnDestroy(): This lifecycle event is also provided by Angular, which is fired immediately before the Angular destroy the view. So all the kinds of stuff like cleanups or unsubscribing from the observables will be done in this event.

So till here, we have discussed the main lifecycle hook events of Angular provided to Ionic and Now, we will be dive into the lifecycle of events of Ionic framework one by one.

1: ionViewWillEnter: In this event, everything is gonna is done behind the scenes, so this event is fired when component routing to is about to animate into view. i.e ionViewWillEnter() lifecycle function signifies that all that process to make your page in full view and transitioned in is about to begin and your page has been queued to be pushed into view.

2: ionViewDidEnter: When this event is fired, your page is completely loaded and completely available or entered into the view alongside the animation. In other words, this event is fired when the component routing to have finished.

3: ionViewWillLeave: This event is fired when your page triggered this lifecycle event in which page gets the information of getting out from the active state of the page but, it is still in the active state only. we can say that this event is Fired when the component routing from is about to animate. This kind of event is useful to start preparing for the things for the next view before navigating to the other view.

4: ionViewDidLeave: This event is fired when the component routing to has finished animating. Now, this event indicated that your ionic page is no longer in focus and leave the active state of the page. Also, this event can only be fired after the ionViewDidEnter event. this kind of events is basically fired to free up the memory resources.

So till now, we have discussed the most important and commonly used lifecycle events of ionic other than this we also have some more lifecycle events which would prevent unauthorized access of the user those events are:

  • ionViewcanEnter which is  Fired before entering into a view, allows you to control whether the view can be accessed or not.
  • ionViewCanLeave which is fired before leaving a view, allows you to control whether the view can be left or not.

But in Ionic4 the above mention two events (ionViewCanEnter and ionViewCanLeave) are replaced with the Routing Guards. for more information about the routing guards visit the official website.

Conclusions: I hope you will be able to understand the basic lifecycle hooks of the Ionic framework and will start implementing it in your application for leveraging the power of these lifecycle events phases and make your app faster and reliable.

Thanks for reading!!!

Written by 

Nitin Arora is a Software Consultant at Knoldus Software LLP. He has done MCA from the Banarsidas Chandiwala Institute of Information technology, Delhi(GGSIPU). He has a graduation degree in BCA from Jamia Hamdard. He has a sound knowledge of various programming languages like C, C++, Java. Also has a deep interest in frontend development like Html, CSS, Angular, Javascript, ionic, react with redux, bootstrap. He is currently working in frontend technologies like React, Html, SCSS, Bootstrap, and Typescript. He is a focused, hardworking, team-oriented member and always exploring new Technologies, His hobbies are to play cricket, volleyball, and do coding.

2 thoughts on “Ionic Page LifeCycle Events4 min read

  1. what is the difference between ionViewWillEnter and ionViewcanEnter and some uses ?

    1. ionViewWillEnter basically Fired when you entering a page, before it about to enter in an active state.It is used for the events that are needed to be done while you leaving the page like: setting event listeners, updating a table, etc.

      On the other hand ionViewcanEnter is comes under the category of Navguard like I have mentioned in my blog, It is fired before entering into a view, allows you to control whether the view can be accessed or not (returning true or false).

Comments are closed.

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading