3

This question and its answers (copied below) provide a solution for handling the back button in Ionic, but that solution only works when other pages are pushed directly from app.component, in which case calling canGoBack and getActive() on this.nav works correctly because other pages have been pushed using this.nav.push in app.component.

However if a page is pushed from one of the pages in the tabs (lets call it page1), i.e. by calling this.navCtrl.push() in page1, then this.nav.canGoBack() in app.components still resolves to false because the push happened using the page1's this.navCtrl.push() not app.component's this.nav.push().

How can I detect inside app.components if a page was pushed from any of pages in the tabs?

 platform.registerBackButtonAction(() => {

        if(this.nav.canGoBack()){
          this.nav.pop();
        }else{
          if(this.alert){ 
            this.alert.dismiss();
            this.alert =null;     
          }else{
            this.showAlert();
           }
        }
      });
    });

  }
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Ari
  • 7,251
  • 11
  • 40
  • 70
  • Maybe you should store main nav in service and push page by that nav. – Duannx Oct 16 '17 at 01:51
  • can you even inject NavController into app.component.ts? can you explain why you need to detect it here? – Philip Brack Oct 17 '17 at 04:54
  • 1
    @PhilipBrack: no, I can't, and I didn't say that. Assume each tab itself is a separate page that has `navCtrl`. On each of these pages you can call `navCtrl.push`, which I don't know how to detect in app.components. I ended up saving them in a service. – Ari Oct 17 '17 at 04:57
  • @Ari Have you handled the issue? Could you answer the problem now? – Naveen Kumar V Sep 24 '20 at 16:52
  • @NaveenKumarV It was long time ago, but I summarized the solution here. Hope it still works for you: https://medium.com/hackernoon/handling-android-back-button-in-ionic-33f7cfbba4b9 – Ari Sep 25 '20 at 00:00

0 Answers0