ionic3处理安卓物理返回键

说明:ionic3 + angular4 处理安卓物理返回键

以下贴出关键代码

 1 import { App, Platform, NavController, AlertController } from 'ionic-angular';
 2 
 3 constructor(
 4  private platform: Platform,
 5  private alertCtrl: AlertController,
 6  private app: App
 7 ) {
 8     platform.ready().then(() = >{
 9         // 注册返回键事件监听
10         platform.registerBackButtonAction(() = >{
11             // 获取当前激活的nav
12             let activeNav: NavController = this.app.getActiveNavs()[0];
14             if (activeNav.canGoBack()) {
15                 // 退回上一级
16                 activeNav.pop();
17             } else {
18                 if (this.alert) {
19                     this.alert.dismiss();
20                     this.alert = null;
21                 } else {
22                     this.showAlert();
23                 }
24             }
25         });
26     });
27 }
28 
29 // 显示确认框
30 showAlert() {
31     this.alert = this.alertCtrl.create({
32         title: '警告',
33         message: '确认退出app吗?',
34         buttons: [{
35             text: '取消',
36             role: 'cancel',
37             handler: () = >{
38                 this.alert = null;
39             }
40         },
41         {
42             text: '确定',
43             handler: () = >{
44                 // 退出app
45                 this.platform.exitApp();
46             }
47         }]
48     });
49 
50     alert.present();
51 }

总结:以上处理逻辑是,当用户在主页面点击安卓返回键后,弹框提示,是否退出app,确定直接退出app,取消则返回主页面

猜你喜欢

转载自www.cnblogs.com/wzq201607/p/ionic3-angular4_back-button.html