Ionic 4: Hardware Back Button Reloading Application

With IONIC 4, there is new method subscribeWithPriority developed to handle race between soft & hard back button. Try modifying your code like below:

 this.platform.backButton.subscribeWithPriority(1, () => {
        navigator['app'].exitApp();
 });

subscribeWithPriority() stops the propagation of the event after its execution and if we subscribe with high priority and execute our prefered navigation instead of default one then it is going to work as you want.

More reference docs for details:
https://github.com/ionic-team/ionic/commit/6a5aec8b5d76280ced5e8bb8fd9ea6fe75fe6795
https://medium.com/@aleksandarmitrev/ionic-hardware-back-button-nightmare-9f4af35cbfb0

UPDATES:

  • Try using this new version of exitApp cordova plugin. I haven't tried myself but looks promising from popularity.
  • Also try to empty the page stack from Navcontroller or go to your home screen, seems like that's causing the reload for app with sidemenu's & tab pages... this.navCtrl.pop() / this._navCtrl.navigateBack('HomeScreen'), and then call exitApp.

NOTE: Tabs & SideMenu as those have its own routing module does create lot of complexity with app navigation.


Solved:

As Mention by @rtpHarry template of SideMenu / Tabs have History which leads application to Reload it self on root page. i was able to solve this by clearing History.

ionViewDidEnter(){
  navigator['app'].clearHistory();    
}

on Your Root Page just Clear your history and your Hardware Back Button will close the Application instead of Reloading it.