Can't unsubscribe properly from a view when using the browser back button


#1

Hi,

I have some problem to clean properly my subscription to event when I use the back button. It seems that the ionViewWillLeave is not called in this case.
Here is my flow :
I enter page 1.

I enter page 2.
In page 2 I do something like that :

ionViewWillEnter()
{

this.events.subscribe(‘tabs:refresh’, this._onRefresh)

}
ionViewWillLeave() {
this.events.unsubscribe(‘tabs:refresh’, this._onRefresh)
}

I press the previous button (the browser one) which make me go back to page 1. The IonViewWillLeave of page 2 is not called.

When I go in page 2 I will subscribe again to my event. The problem is nothing has cleaned my previous subscribe so I end up having the function called twice when this events is triggered.

I am not able to find a function that is called when using the brower previous button. Is there one ?

I tried to call this.events.unsubscribe(‘tabs:refresh’, this._onRefresh) in my view constructor to clean my mess but it doesn’t seems to work either.

Thanks,


#2

Hi. Don’t rely on ion Events because they are tied to Ionic navigation. Use it if you are certain that the browsers’ buttons won’t be used. Otherwise try implementing OnDestroy in your component.
Hope it helps. Best regards,
J


#3

Yeah that helps. I guess I need to unsubscribe in the ionViewWillLeave and ngOnDestroy. I just dont like when I need to do something that is not symetrical.

Thanks.