Hello all,
in the tutorial for ionic 2 ionic 2 tutorial after clicking on an item the back button closes the app instead of going back to the list page I tried using platform
registerBackButtonAction but it didn’t work. here is the code I tried
import {Page, NavController, NavParams, Platform} from 'ionic-framework/ionic';
import {Inject} from 'angular2/core';
@Page({
templateUrl: 'build/pages/item-details/item-details.html'
})
export class ItemDetailsPage {
constructor(@Inject(NavController) nav, @Inject(NavParams) navParams,
@Inject(Platform) platform) {
this.nav = nav;
// If we navigated to this page, we will have an item available as a nav param
this.selectedItem = navParams.get('item');
this.platform = platform;
console.log(this.platform);
this.platform.registerBackButtonAction(function(){
},500);
// this.platform.onHardwareBackButton(function(e){
// e.preventDefault();
// return true;
// });
}
}
I think this should be the most basic feature and important. Android users are used to the back button, and will just stop using the app if it closes on pressing the back button.
I am considering moving to ionic, and will not publish my app unless I am able to implement this.
Or you could watch for the document event for back button and use it as you need, for example here’s mine from the init() method in the app.ts file inside platform.ready().then(() => {...}):
// ------------ Double back button press to exit---------------
document.addEventListener('backbutton', () => {
if (this.nav.canGoBack()) {
this.nav.pop()
return;
}
if(!this.backPressed) {
this.backPressed = true
window.plugins.toast.show('Presiona el boton atras de nuevo para cerrar', 'short', 'bottom')
setTimeout(() => this.backPressed = false, 2000)
return;
}
// this.platform.exitApp()
navigator.app.exitApp()
}, false);
Hi,
I have the same problem, i use ionic 2 beta 8 and here is my code:
this.platform.ready().then(() => {
this.hideSplashScreen();
this.platform.registerBackButtonAction(function(event){
let nav=this.app.getComponent('nav');
if (nav.canGoBack()) {nav.pop();}
else {this.confirmExitApp(nav);}
},101);
// Okay, so the platform is ready and our plugins are available.
// Here you can do any higher level native things you might need.
StatusBar.styleDefault();
});
i tried on real device and the hardware back is disabled, it does nothing.
Any help please. I googled a lot for any example of use of registerBackButtonAction in ionic 2 but no result
Maybe the app isn’t yet ready? Does it help to test the platform first?
For the record, to override the back button I did the following, works fine for me:
onPageWillEnter() {
this.platform.ready().then(() => {
this.unregisterCustomBackActionFunction = this.platform.registerBackButtonAction(() => {
// The special stuff I want to do and where I want to go
});
});
}
and then later, before leaving my view, to set back the back action to her default behavior