ionic2如何處理android硬件返回按鈕
問題
注冊安卓硬件返回按鈕事件是必須的,因?yàn)橛脩舨恍⌒狞c(diǎn)擊了返回按鈕就退出app體驗(yàn)很不好,所以有幾種方法:
1.實(shí)現(xiàn)按返回鍵最小化應(yīng)用(最小化應(yīng)用需要裝cordova-plugin-appminimize插件,使用window['AppMinimize'].minimize();)。
2.要么請求用戶確認(rèn)(添加一個Confirmation Alerts)。
3.按一下提示,按兩下退出(加一個方法用toast提醒)。
這里用第三種展示。
解決
在app.html中,添加#myNav,在app.component.ts文件通過@ViewChild('myNav')獲?。?/p>
<ion-nav #myNav [root]="rootPage"></ion-nav>
在app.component.ts中:
import {Component, ViewChild} from '@angular/core';
import {Platform, ToastController, Nav, IonicApp} from 'ionic-angular';
import {StatusBar, Splashscreen} from 'ionic-native';
import {TabsPage} from '../pages/tabs/tabs';
@Component({
templateUrl: 'app.html'
})
export class MyApp {
rootPage = TabsPage;
backButtonPressed: boolean = false; //用于判斷返回鍵是否觸發(fā)
@ViewChild('myNav') nav: Nav;
constructor(public ionicApp: IonicApp, public platform: Platform, public toastCtrl: ToastController) {
platform.ready().then(() => {
StatusBar.styleDefault();
Splashscreen.hide();
this.registerBackButtonAction();//注冊返回按鍵事件
});
}
registerBackButtonAction() {
this.platform.registerBackButtonAction(() => {
//如果想點(diǎn)擊返回按鈕隱藏toast或loading或Overlay就把下面加上
// this.ionicApp._toastPortal.getActive() || this.ionicApp._loadingPortal.getActive() || this.ionicApp._overlayPortal.getActive()
let activePortal = this.ionicApp._modalPortal.getActive();
if (activePortal) {
activePortal.dismiss().catch(() => {});
activePortal.onDidDismiss(() => {});
return;
}
let activeVC = this.nav.getActive();
let tabs = activeVC.instance.tabs;
let activeNav = tabs.getSelected();
return activeNav.canGoBack() ? activeNav.pop() : this.showExit();//另外兩種方法在這里將this.showExit()改為其他兩種的方法的邏輯就好。
}, 1);
}
//雙擊退出提示框
showExit() {
if (this.backButtonPressed) { //當(dāng)觸發(fā)標(biāo)志為true時,即2秒內(nèi)雙擊返回按鍵則退出APP
this.platform.exitApp();
} else {
this.toastCtrl.create({
message: '再按一次退出應(yīng)用',
duration: 2000,
position: 'top'
}).present();
this.backButtonPressed = true;
setTimeout(() => this.backButtonPressed = false, 2000);//2秒內(nèi)沒有再次點(diǎn)擊返回則將觸發(fā)標(biāo)志標(biāo)記為false
}
}
}
在tabs.html中,添加#mainTabs,在tabs.ts文件通過@ViewChild('mainTabs') tabs:Tabs;獲取:
<ion-tabs #mainTabs> <ion-tab [root]="tab1Root" tabTitle="Home" tabIcon="home"></ion-tab> <ion-tab [root]="tab2Root" tabTitle="About" tabIcon="information-circle"></ion-tab> <ion-tab [root]="tab3Root" tabTitle="Contact" tabIcon="contacts"></ion-tab> </ion-tabs>
在tabs.ts中:
import {Component, ViewChild} from '@angular/core';
import { HomePage } from '../home/home';
import { AboutPage } from '../about/about';
import { ContactPage } from '../contact/contact';
import {Tabs} from "ionic-angular";
@Component({
templateUrl: 'tabs.html'
})
export class TabsPage {
@ViewChild('mainTabs') tabs:Tabs;//加這句以及引用兩個模塊
tab1Root: any = HomePage;
tab2Root: any = AboutPage;
tab3Root: any = ContactPage;
constructor() {
}
}
完成。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SwipeLayout框架實(shí)現(xiàn)側(cè)拉刪除編輯功能
這篇文章主要為大家詳細(xì)介紹了SwipeLayout框架實(shí)現(xiàn)側(cè)拉刪除編輯功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-08-08
Android FlowLayout流式布局實(shí)現(xiàn)詳解
這篇文章主要為大家詳細(xì)介紹了Android FlowLayout流式布局的實(shí)現(xiàn)方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-09-09

