-
Notifications
You must be signed in to change notification settings - Fork 13.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement platform hardware back button functions #5071
Comments
From @adamdbradley on November 7, 2015 16:1 Yes Ionic be able to handle this like it did in v1, just haven't completed it yet. |
Agree, nice to have |
FYI I'm using something like this as a workaround, until Ionic 2 provides something. Seems to get the job done... Not entirely generic though. This particular app has a sub-page with a Tabs component, i.e. multiple NavControllers are involved - hence some additional complexity. (If a Modal is active, the back button still exits the app when using this code) app.js
tabs.js
tabs.html
|
This should've been in beta.4 but I guess we created a new issue for it. Closed the other issue as a dup. |
I tried the workaround from tomsun, but that didn't work out for me. This did for the moment: document.addEventListener('backbutton', () => { |
@brandyscarney I was excited when I thought it's coming in beta.3 😞 |
The workaround proposed above by @tomsun is a great idea. Thanks. |
I slightly modified the workaround of @tomsun to close modals. Hope it will be helpful. app.js, 'backbutton' handler
|
I'm hoping this will also include the newly added Windows Phone platform as well. |
The workaround seems to stop working on beta.4 The line: |
I'm on beta.6 and the back button is mostly working great. If your navigation stack has a page to go back to then the hardware back button does take you there. On the other hand, if you're at a root page then hitting the back button closes the app. I think it would be better if on root pages, rather than exiting the app it did nothing at all, or prompt the user to close the app or not. My preference would be for the back button to be non-functional on root pages. |
@SP1966 - yep my issue is exactly what you've describe. I vote for 2 options:
|
@SP1966 Normal behavior on Android is that the app will close if you can't pop any more activities and hit back. And so do I think ionic should work as well. If you have a different need the behavior can be overridden. |
That's exactly the workaround I made. So when you're on a root page down http://www.codingandclimbing.co.uk/blog/ionic-2-android-back-button-13 Although this is now kinda broken as of beta 6... Just food for thought!
|
By default we can keep ionic closing the app. But we should be able to easily override default behavior. |
See here for more information or read up on why overriding standard user interactions is a Bad Idea in mobile apps. |
It seems like a current implementation of a back button (beta.6) does not take into account a case when an active tab has its own non-empty |
@mnasyrov - oooo yeah :) +1 |
any update? |
There is a separate issue open for controlling the behavior of the back button: #6556 If you have any feedback for controlling the behavior please place it on that issue. If you believe there is a bug with the current implementation please create a new issue. Thanks! |
Created #6574 about closing tab's pages and modals. |
@SP1966 your back button work on beta 6? i'm in android and a cannot make this work. Please what you did? |
How to implement a hardware back button functionality for android in ionic2? |
Well.. i have this method on the platform.ready();
the app is null, in my constructor i try;
but dont work :(. this is the Dave Shirman workaround : http://www.codingandclimbing.co.uk/blog/ionic-2-android-back-button-13#comment-2690874526 |
I think that getComponent is gone as of beta 7. Check the Ionic blog post
|
Ok |
on Beta 7, app always close when hit back button even we put any idea? Thanks |
In Beta7 the app still exits when a modal is opened and we hit back button. Should we reopen that? |
I think this should be open, currently cannot stop the back button from closing the app without displaying any prompt. Perhaps there is a way to override the default back button handler but yet to be documented? |
For now, you can comment these lines on // platform.backButton.subscribe(function () {
// var activeNav = _this.getActiveNav();
// if (activeNav) {
// if (activeNav.length() === 1) {
// platform.exitApp();
// }
// else {
// activeNav.pop();
// }
// }
// }); Now you can doing something like this from this.platform.backButton.subscribe(() => {
this.menu.close();
if (this.nav.canGoBack()) {
this.nav.pop();
return;
}
let activePage = this.nav.getActive().instance;
let whitelistPages = [AuthPage, DashboardPage];
// if current page is not in whitelistPage
// then back to DashboardPage first
if (whitelistPages.indexOf(activePage.constructor) < 0) {
this.nav.setRoot(DashboardPage);
return;
}
if (! this.backPressed) {
this.backPressed = true
showToast('Press again to exit', 2000);
setTimeout(() => this.backPressed = false, 2000)
return;
}
this.platform.exitApp();
}, error => showToast(error)) For sample app you can download from https://play.google.com/store/apps/details?id=com.ferrisutanto.ezygriya You need to press back button twice under 2000ms to exit app .. |
Thank you @ghprod , your method works for now. I am still suggest to open the issue for making the default back button handler be able to be overridden. There would be a lot of ways to do so, like allowing the handler function be passed by the config during bootstrap, only use the default one when such does not presents. Despite exposing the subscriber via getter (e.g. |
Yes, agree with you .. 👍 |
@shprink I have the same issue, if i hit the back button when the modal is open, the app exits even before executing the this.platform.backButton.subscribe(..., @mhartington Any suggestions? i am using beta.7 |
Hello, I'm using ionic version 2.0.0 when i press the back button it closes the app. I have something like this to perform an event on click of back button $ionicPlatform.onHardwareBackButton(callToAFunction); But this is not working instead of calling the function it closes the app. Any suggestions? |
I found cordova-plugin-backbutton for that. |
@mhartington can this be closed? Should have been resolved here #8692 |
There is missing ts binding in ionic 2 for navigator.app.overrideButton() cordova function. |
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. |
From @ydeshayes on November 6, 2015 11:3
Hello,
when i tap the android (hardware or not) back button, the app closed. I see in the code that a function is predicted but not implemented (https://github.com/driftyco/ionic2/blob/076dc7645edfd2d216a8f0f7bf52abca6b6be35e/ionic/platform/platform.ts line 161).
Did you think about integrate this back button with the navigation behavior ? (touching the android back button call navigation.pop() for example) or do we have to implement it ourselves ?
thanks !
Copied from original issue: driftyco/ionic2#462
The text was updated successfully, but these errors were encountered: