問題:安卓的物理返回按鈕有時有效果有時沒效果
方法:可能是獲取設備類型的方法是異步的,沒有正確返回設備號。
下面是代碼:
import { NavController, ActionSheetController, AlertController, MenuController, ModalController, PopoverController, Platform } from '@ionic/angular';
constructor(public navController: NavController,
public eventService: EventService,
public alertCtrl: AlertController,
public modalCtrl: ModalController,
public menuCtrl: MenuController,
public actionSheetCtrl: ActionSheetController,
public popoverCtrl: PopoverController,
public platform: Platform) { }
ngOnInit(): void {
this.androidBackButtonAction();
}
androidBackButtonAction() {
this.platform.ready().then(() => {//獲取設備異步
if (this.tool.devicePlatformType() == 2) {
this.platform.backButton.subscribe(() => {
this.tabsCanGoBack = this.tabs.outlet.canGoBack();
this.tabsParentCanGoBack = this.tabs.outlet.parentOutlet.canGoBack();
this.androidBackButtonHandle();
});
}
})
}
async androidBackButtonHandle() {
try {
const alert = await this.alertCtrl.getTop();
if (alert) {
alert.dismiss();
return;
}
const action = await this.actionSheetCtrl.getTop();
if (action) {
action.dismiss();
return;
}
const popover = await this.popoverCtrl.getTop();
if (popover) {
popover.dismiss();
return;
}
const modal = await this.modalCtrl.getTop();
if (modal) {
modal.dismiss();
return;
}
const isOpen = await this.menuCtrl.isOpen();
if (isOpen) {
this.menuCtrl.close();
return;
}
if (!this.tabsCanGoBack && !this.tabsParentCanGoBack) {
this.tool.appMinimize();
return;
}
} catch (error) {
}
}