V4 : back-button doesn't exit app [solved--Tutorial]

This worked for me too. :slight_smile:

If you need to use hardware back button navigation with tabs:

private listenOnHardwareBackButton() {
    if (this.platform.is('android')) {
        this.platform.backButton.subscribeWithPriority(0, () => {
            if (!this.counter) {
                const appStr = 'app';
                navigator[appStr].exitApp();
            }
            this.navCtrl.back();
        });
        this.router.events
            .pipe(filter(event => event instanceof NavigationStart))
            .subscribe((event: NavigationStart) => {
                if (event.navigationTrigger === 'popstate') {
                    this.counter--;
                } else {
                    this.counter++;
                }
            });
    }
}
1 Like

what does mean [‘app’]

This solution works from version ionic/angular 4.7.2 https://github.com/ionic-team/ionic/releases/tag/v4.7.2
I was on 4.6.1 and no events were firing

can you tel me where to implement this code, im failing to implement it

Hardware back button is called globally even if you use in specific page. So best place to use it is in app component.

backButtonEvent() {
    if (this.platform.is('android')) {
        this.platform.backButton.subscribeWithPriority(0, () => {
        console.log('this.router.url', this.router.url);
        if (this.router.url === '/home' ) {
           navigator['app'].exitApp();
        }  else {
         this.navCtrl.navigateBack('/' + 'componetToGo');
        }
      });
    }
  }
1 Like

And where do we call “backButtonEvent()” method in the app?

Basically you are registering the back button event so you can call this from appComponet ngOnit().

Hey Still facing issue …after applying your solution from every page whene ever we press back button app goes exit.

Please can you provide more information about this problem plz

You have just to apply this on the homepage or the page from where you want the user to be able to quit the app.
Not on all the pages !

On entering the page, the subscription makes it possible. On leaving, the unsubscribe prevents to exit the app from the other pages

HI,
in AppComponent -> initializeApp

add
this.platform.backButton.subscribeWithPriority(0, () => {
navigator[‘app’].exitApp();
});
Like this :

  initializeApp() {
    this.platform.ready().then(() => {
      this.statusBar.styleDefault();
      this.splashScreen.hide();
      this.platform.backButton.subscribeWithPriority(0, () => {
        navigator['app'].exitApp();
       
     });
    });
  }
1 Like

first time it works fine but then when i come back to my root page and press back button it does nothing

do you subscribe when you arrive on the page (ionwiewwillenter)?

i subscribe in constructor

I think constructor is called just once, try to move the subscription to ionviewwillenter which is called every time the page appears

works fine on emulator but not after creating apk

sorry, no clue for that issue…