ionic-framework: bug: IonRouterOutlet canGoBack() return always return false

Bug Report

Ionic version:

[x] 4.6.1 / 4.6.2

Current behavior:

ActivatedRoute canGoBack() always return false

Expected behavior:

canGoBack() should return correctly

Steps to reproduce:

Related code:

insert short code snippets here

Other information:

Ionic info:

insert the output from ionic info here

About this issue

  • Original URL
  • State: open
  • Created 5 years ago
  • Reactions: 8
  • Comments: 18 (5 by maintainers)

Most upvoted comments

I took a closer look at the problem and it seems that routerOutlet always fetches the next parent instance of ion-router-outlet from the respective page. So if we call routerOutlet.canGoBack() inside app.component.ts, the value would always be false because ion-tabs have their own ion-router-outlet.

Thats why using this.routerOutlet.canGoBack() inside childpages of ion-tabs will return the correct boolean value.

If you need to handle something globally (like using android hardware backbutton to exit app) you can use following snippet:

@ViewChild(IonTabs, { static: true }) private ionTabs: IonTabs;
...
constructor(
  private platform: Platform,
) {
  this.platform.backButton.subscribeWithPriority(-1, () => {
    if (!this.ionTabs.outlet.canGoBack()) {
      App.exitApp();
    }
  });
}

It’s important that you handle the logic inside tabs.page.ts since we use the Viewchild of ion-tabs, which contains the outlet for tabbed pages.