用ionic做了一个tabs的程序,但是在安卓机上测试发现,只要一按返回键程序就退出了,再按程序就会重新加载,这样的交互效果非常的不友好,于是查找文档找到解决方法,当按返回键时弹出个对话框提示,用户选择退出才真的退出程序,否则按返回键则不进行任何操作。
只需要更改app.components.ts文件,具体的代码如下所示:
export class MyApp {
rootPage:any = HomePage;
//程序默认在platform: Platform前没加public,记得加哦
constructor(public platform: Platform, statusBar: StatusBar, splashScreen: SplashScreen,
public alertCtrl: AlertController) {
platform.ready().then(() => {
// 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();
splashScreen.hide();
//注册返回按钮的处理方法
platform.registerBackButtonAction(() => {
this.showPrompt();
return false;
});
});
}
//退出提示框,按确定按钮才退出
showPrompt() {
let prompt = this.alertCtrl.create({
title: '确认退出吗?',
message: "感谢您对本软件的支持!",
buttons: [
{
text: '取消',
handler: data => {
}
},
{
text: '退出',
handler: data => {
this.platform.exitApp();
}
}
]
});
prompt.present();//弹出提示框
}
}
主要是要调用platform的registerBackButtonAction方法,对其进行处理。
如果觉得写的不错,可以打赏哦,您的支持是我一直努力的动力!
