Does Ionic 5 call ngOnDestroy for Services and home page when app is closed

450 views Asked by At

In my Ionic 5 project, I have ngOnDestroy() for the service classes to clean up data. I also have ngOnDestroy() on my Home page. When I do the following navigation from the Home page to Page A and back to the Home page the ngOnDestroy() for Page A is triggering.

 - Home page -> Page A 
      
 - Home page <- Page A

The issue is when I close the app I am expecting the ngOnDestroy() on the Home page and all the service classes to be triggered but that is not happening. I see the following is mentioned in the Ionic documentation.

ngOnDestroy will only fire when a page "popped".

I checked that even ionViewWillLeave is not triggering on Home page when the app is closed. Please suggest how do I do clean-up activity in the Home page and services?

1

There are 1 answers

0
Norbert Bartko On

As far as i know you can use, for websites, the window beforeunload event but it is not reliably fired.

This post answers also recommend using beforeunload in cordova when you run inside an app.