问题:安卓的物理返回按钮有时有效果有时没效果
方法:可能是获取设备类型的方法是异步的,没有正确返回设备号。
下面是代码:
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) {
}
}