Ionic-framework: Ionic v2 should have a way to control hardware backButton

Created on 18 May 2016  路  13Comments  路  Source: ionic-team/ionic-framework

Short description of the problem:

In ionic v1, we can use following code to prevent backButton from closing app:
$ionicPlatform.registerBackButtonAction(function (e) {
//Prevent the hardware backButton from closing the app
e.preventDefault();
return false;
});

In v2, there is no way to do this, return false doesn't prevent from closing.
platform.backButton.subscribe(function(){
return false;
})

Which Ionic Version? 1.x or 2.x
2.x

Most helpful comment

I want give user a confirmation toast "Press back button again to exit app"

All 13 comments

For ionic 2 how can I get that functionality? (hardware back buttton for android)

+1

@GaneshPediredla it is now inplemented in the core (I think since beta5), but no way to override it. In my case I want to minimize the app instead of exiting the app when the root of the stack is reached.

I want give user a confirmation toast "Press back button again to exit app"

Yeah, previously this was working if you just listened to document.addEventListener('backbutton'), but now this is simply not working at all. This needs to be fixed as currently there's no way to intercept the behaviour at all to customise it. Very important.

I can work on a PR for this feature, but I'm trying to determine if it would be better to allow overriding the default behavior by passing a replacement function, or just disable the default close behavior via a config value? Any recommendations?

I think being able to override the function is more desirable.

Hello all! This will be in the next release (beta.9) with the new registerBackButtonAction method. Thanks for using Ionic everyone!

Please reopen this issue as it is not working in beta10 even tried document.addEventListener('backbutton')

I have a weird issue, the event listener works if I debug the app ionic run android -c but without -c it doesn't seem to work. Also with the live reload does not work

It will only work if the user navigates to a second page. Not working at root page when the user has just opened the app. I am running the app using the command:ionic cordova run android -l -c

I believe I'm having the same issue as Eonio. registerBackButtonAction() is reached, but the Back event occurs regardless of the code I have, quitting the app. My console.log can be seen, but my confirmExit() dialog does not appear - the app just minimises, regardless of the page I'm on (root or otherwise).
I'm testing with 'ionic cordova run android -cl', but the release build does the same; this was working fine, up until we upgraded from Ionic 3.4.0 to 3.7.0, so something seems to have gone awry, unless I've missed something.

Update: Ick... for me it's almost certainly because of googlemaps plugin, v2 beta 3 :(
https://forum.ionicframework.com/t/hardware-back-button-exiting-app-even-after-overriding-the-registerbackbuttonaction-event/101545/3

Thanks for the issue! This issue is being locked to prevent comments that are not relevant to the original issue. If this is still an issue with the latest version of Ionic, please create a new issue and ensure the template is fully filled out.

Was this page helpful?
0 / 5 - 0 ratings