0

When navigating from one page to another using nav.setRoot(page), why does the iOnViewWillLeave event handler of the current page capture the leaving event after the iOnViewDidLoad of the target page already executes?

I need to unsubscribe from an event when the view is left and then subscribe to that event again upon entering the target page however it is currently unsubscribing from the event AFTER entering the target page which results in my page not listening to the event.

Workflow

  1. User In Page A
  2. User Navigates to Page B

How can I capture the leaving event in step 3 before step 2 happens?

Result

1)Entering Page A.ts

2)Entering Page B.ts

3)Leaving Page A.ts

Page A.ts

ionViewDidLoad(){
    console.log("Entering Page A.ts")
}

ionViewWillLeave(){
    console.log("Leaving Page A.ts)
} 

Page B.ts

ionViewDidLoad(){
    console.log("Entering Page B.ts")
}
ionViewWillLeave(){
    console.log("Leaving Page B.ts)
} 
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
Yeo Bryan
  • 331
  • 4
  • 24
  • What are you subscribing to where the sequence of subscribe/unsubscribe of two pages matters? – Suraj Rao May 20 '22 at 06:07
  • I am subscribing to an event on the individual ts files to handle the routing process when a notification is received and clicked on. IE, if the notification will ultimately bring the user to the page which he is currently on upon clicking it, instead of routing to the user to that page it should just call a refresh – Yeo Bryan May 20 '22 at 06:10
  • Maybe try using behaviourSubject to update data instead of calling refresh? https://stackoverflow.com/a/55740343/4826457? Subscribe in app component to navigate to page and also in the particular page to refresh if already there – Suraj Rao May 20 '22 at 06:38

1 Answers1

0

use these two lifecycle event instead of them

ionViewWillEnter(){
 console.log("starting page");
}
ionViewWillLeave(){
 console.log("Leaving page");
}
Tiwari
  • 45
  • 2