ionic+cordova

ionic4处理android返回按钮事件

2019-03-22  本文已影响0人  昵称已被使用_

前言

效果演示

处理逻辑

核心代码

import { Component, OnInit, ViewChild } from '@angular/core';
import {
    ActionSheetController,
    AlertController,
    Events,
    IonTabs,
    MenuController,
    ModalController, Platform,
    PopoverController
} from '@ionic/angular';
import { Helper } from '../../providers/Helper';
import { NativeService } from '../../providers/NativeService';

@Component({
    selector: 'app-tabs',
    templateUrl: 'tabs.page.html',
    styleUrls: ['tabs.page.scss']
})
export class TabsPage implements OnInit {
    @ViewChild('tabs') tabs: IonTabs;
    tabsCanGoBack = false;
    tabsParentCanGoBack = false;

    constructor(public platform: Platform,
                public events: Events,
                public helper: Helper,
                public native: NativeService,
                public alertCtrl: AlertController,
                public modalCtrl: ModalController,
                public menuCtrl: MenuController,
                public actionSheetCtrl: ActionSheetController,
                public popoverCtrl: PopoverController) {
    }

    ngOnInit() {
        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.native.appMinimize();
                return;
            }
        } catch (error) {
        }
    }
}

需要注意的问题

            // 先判断是否能返回,不能返回则认为是根页面
            this.tabsCanGoBack = this.tabs.outlet.canGoBack();
            this.tabsParentCanGoBack = this.tabs.outlet.parentOutlet.canGoBack();
           // 处理弹出层
            this.androidBackButtonHandle(); 
            // 如果有弹出层打开,则关闭并return
            const alert = await this.alertCtrl.getTop();
            if (alert) {
                alert.dismiss();
                return;
            }
           ......
            // 如果没有弹出层打开则最小化app
            if (!this.tabsCanGoBack && !this.tabsParentCanGoBack) {
                this.native.appMinimize();
                return;
            }

最后

上一篇下一篇

猜你喜欢

热点阅读