Is the hardware back button working with ionic 4? I’ve started a project using the sidemenu starter template. When running ionic serve, going back works (pressing the back button) on the browser. Then I try running
ionic cordova prepare android
ionic cordova run android -l
The app loads, the items on the menu work but using the hardware back button doesn’t. It doesn’t go anywhere. Is it intended? Do I need to code it myself? I haven’t found any information regarding this.
Could you make sure that it’s not working? I found it weird that it’s not working and I was (am) going crazy. I thought that maybe I was doing something wrong with the project I had already finished but then I created a project using one of their starter templates. No luck.
The registerBackButton disappeared but now you have to subscribe.
this.platform.backButton.subscribe(() => {
// this does work
});
try this if you want to use a hardware back button to navigate back in your app. put following code in app.component.ts
var lastTimeBackPress = 0;
var timePeriodToExit = 2000;
platform.registerBackButtonAction(() => {
// get current active page
let view = this.nav.getActive();
if (view.component.name == "HomePage") {
//Double check to exit app
if (new Date().getTime() - lastTimeBackPress < timePeriodToExit) {
platform.exitApp(); //Exit from app
} else {
let toast = this.toastCtrl.create({
message: 'Press back again to exit App',
duration: 3000,
position: 'bottom'
});
toast.present();
lastTimeBackPress = new Date().getTime();
}
} else {
// go to previous page
this.nav.pop({});
}
});
Thank you for the suggestion. i remember doing this on v1 but in v4 the back button doesn’t seem to work, at least for me. Not sure if an Ionic v4 issue, the cordova-plugin-ionic-webview (2.0.0) or cordova (6.4.0). Plus, besides pages I’m also using alerts and popovers (as customizable popups, modals didn’t work the way I wanted).
And the code you are suggeting works in v3 but not in v4 because the registerBackButtonActino was removed in favor of subscribing to an… observable (? i think).
I had to try it because he said it’s working fine on his project.
Don’t worry. I’m working on an alternative just in case the back button doesn’t actually work. Not sure if it’s an ionic v4 issue, its webview plugin or cordova.
But could you confirm that the back button in a device is not working as the back button in the browser? Go to a page, press the back button and doesn’t go back. Before I didn’t need to do anything for the back button to work with its default behaviour, but now maybe we have to do it ourselves?