@cherzog that’s exactly what I’m using. When I inspect the device via devtools and go to the console I can see the logs when back button on Android is pressed. I can also minimize the app using AppMinimize.
Maybe it has to do with you using it on a modal page? Modals always seem to have issues. Try doing it in something that isn’t a modal (ie home page or app component like suggested above).
Also, why are you putting this in a modal? I’m having a hard time understanding what a use case would be.
It’s the modal, yes. It also happens when you use another overlay. A select, for example. You have the sub-page open, open the Select and press the Back button. The subpage disappears and the select overlay remains open. I’ve updated the git hub repo …
Am i doing something wrong? In app.component i have the following code, still nothing shows on console in devtools(chrome) when back button pressed on device.
UPDATE: This solution actually works, i was just running the app on device in debug mode with “ionic cordova run android --device -l --debug” command and i guess that’s messed it up. After running it without --debug it worked like a charm.
And it works…
But it also works in the other pages, so instead of having the normal “back” behaviour, the backButton exits the app from everywhere in the app…
I had the idea of unsubscribing when moving from the homepage, but the problem is that when I go back to the home page, the ngOnInit is not fired, so I can’t resubscribe…
Another idea is to access the name of the page where the user is (router??) from the backButtonEvent function, to know which behaviour to have
Any solution so far? I still have the same problem, being on the root page the back button does not work to close the application, and add navigator [“app”]. exitapp () the application is left on any page that is
try {
const element = await this.menu.getOpen();
if (element !== null && element !== undefined)
this.menu.close();
return;
}
} catch (error) {
}
please note updated line here if (element !== null && element !== undefined) because menu.getOpen() does not return null as per it’s documentation and always exceutes this if block regarless of menu behaviour so code after this try catch block is always skipped. instead its undefined when menu is closed. I think its bug
Works fine, but having one issue. Let’s say my App’s home page is Services.
Now I’m checking this 3 case:
If i’m on Services page, pressing back-button then it shows toast. => WORKS AS EXPECTED
If i navigate to Login page from Services, then In Login page, i’m pressing back-button then it goes back to Services page and shows toast. => NOT WORK AS EXPECTED
(It should not display toast)
If i navigate to Register page from Login, then In Register page, i’m pressing back-button then it goes back to Login page and again it goes back to Services page. => NOT WORK AS EXPECTED
(It should only navigate to Login page)
I get error in routerOutlets.forEach. vendor.js:51643 ERROR Error: Uncaught (in promise): TypeError: this.routerOutlets.forEach is not a function TypeError: this.routerOutlets.forEach is not a function at AppComponent.<anonymous> (main.js:1130) at step (vendor.js:106764) at Object.next (vendor.js:106745) at fulfilled (vendor.js:106735) at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invoke (polyfills.js:2749) at Object.onInvoke (vendor.js:53218) at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invoke (polyfills.js:2748) at Zone.push../node_modules/zone.js/dist/zone.js.Zone.run (polyfills.js:2508) at polyfills.js:3247 at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (polyfills.js:2781) at resolvePromise (polyfills.js:3189) at polyfills.js:3099 at fulfilled (vendor.js:106735) at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invoke (polyfills.js:2749) at Object.onInvoke (vendor.js:53218) at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invoke (polyfills.js:2748) at Zone.push../node_modules/zone.js/dist/zone.js.Zone.run (polyfills.js:2508) at polyfills.js:3247 at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (polyfills.js:2781) at Object.onInvokeTask (vendor.js:53209)
Never mind that, I forgot to change ViewChild to ViewChildren but now I have a problem when i dismiss a page. The outlet.pop() and the Toast function triggers at the same time.
constructor(){
this.platform.backButton.subscribe(async() => {
if (new Date().getTime() - this.lastTimeBackPress < this.timePeriodToExit) {
navigator[‘app’].exitApp(); // work in ionic 4
} else if (this.router.url === ‘/tabs/tab1’) {
this.matBar.open(‘Press back again to exit App’, ‘2000’, {
duration: 2000,
});
this.lastTimeBackPress = new Date().getTime();
}
});
}
Hi, I have to handle same scenario with hardware back button to exit the app when user clicks back button from home page and should not take it to login page as user already logged In. I tried your solution placing code in app.component.ts but it did not work for me… Could you please help in getting this done by providing sample application code(complete application)
export class AppComponent { @ViewChild(IonRouterOutlet) routerOutlet: IonRouterOutlet;