How to destroy current page on ionViewDidLeave


#1

Hello guys,

I figured when I navigate to other page on Ionic app, the previous page is still alive and running.

In my app, each HTML page has all features and when I navigate to other page, the previous page is no longer necessary.

Is there any way to completely stop previous page from running in background when navigate other page?

I think there must be setting which works with ionViewDidLeave()


#2

No. It stays live until it’s popped off the nav stack. If you need to queue the page for destruction, you could setRoot the new page. But that only means destruction will eventually happen, not that it happens immediately. So in your ionViewWillLeave make sure you do teardown of the page, like unsubbing from all Observables.


#3

I see… so it will remain in background.
Yeah, the best bet is taking all ram consuming features off of current page on ionViewWillLeave.
Eventually I would like to program everything on one HTML file and not use any additional page to avoid this but that will need a lot more complicated programming than what I can do now. Maybe I will try that with Ionic 4 in the future.

Thanks,


#4

The downside of that is you need to be clear what the back buttons do. The page stays active by default so the back button can load the previous page right away.


#5

Yes, my pages don’t have a back button but Android provides a stock back button.
How many old pages does Ionic keep by default?
That raises a concern here… it’s likely that I must remove old pages on leave or I should deactivate android stock back button.


#6

Your phone does. So you need to handle the hardware back button at the very least. (And in a desktop browser a user can easily jump back three pages, so how do you want to handle browser history? If you don’t plan on building for the desktop, not an issue.)

You’ve probably skimmed the NavController docs before. Read them again, this time reading every method. Now that you’re running into this issue, it will make a lot of sense.


#7

Thanks for detailed answers.
Yes, this is for Android only.
I will see what I can do. The best method would be deactivating stock android back button if that’s possible.


#8

Disabling android hardware button is possible on Ionic.

This is an old ionic example but it’s possible so I can simply take off all ram consuming features & files from each page when they navigate to other page and disable android hardware back button so the previous page can’t be seen again.