离子恢复暂停事件防止文件触发仅按下主页按钮时触发

时间:2017-02-16 19:13:59

标签: cordova ionic-framework cordova-plugins

我正在开发一个聊天应用程序,我正在暂停和恢复活动中使用。

document.addEventListener('暂停',暂停,false);

document.addEventListener(' resume',onresume,false);

当我打开应用程序并按下主屏幕按钮到Android手机时,此活动非常完美。 但我的问题是,在聊天应用程序中,当我选择浏览按钮电话图像库时,我从图库发送文件附件,同时暂停事件是火。当我在图像库中没有选择任何图像时,当我点击主页按钮时,同一事件没有被触发。因此,如何在从库中选择文件时阻止暂停事件。

ionic v1 中还有其他方法吗?或者我如何在暂停和恢复活动中开火。

3 个答案:

答案 0 :(得分:2)

我写了一个小服务来解决这个问题:

import {Injectable} from '@angular/core';
import {Subject} from "rxjs/Subject";

@Injectable()
export class EventService {
    protected resumeHalted = false;
    protected resumeSubject = new Subject<any>();

    protected resumeListener = () => {
        if (this.resumeHalted) {
            return;
        }
        this.resumeSubject.next();
    };

    constructor() {
        document.addEventListener('resume', this.resumeListener);
    }

    haltResume() {
        this.resumeHalted = true;
    }

    continueResume() {
        this.resumeHalted = false;
    }

    resume() {
        return this.resumeSubject;
    }
}

图库电话也包含在服务中。每次我打电话,我都会停止&#34;事件和&#34;继续&#34;在用户交互完成后:

getPicture(options: CameraOptions) {
    let subject = new Subject<any>();

    this.eventService.haltResume();
    this.camera.getPicture(options).then((path) => {
        // ...
        subject.next();
        subject.complete();
        this.eventService.continueResume();
    }, () => {
        this.eventService.continueResume();
    });

    return subject.asObservable();
}

最后一步:我没有收听简历活动,而是订阅简历Oberservable:

        this.eventService.resume().subscribe(() => {
            this.statusBar.hide();
        });

答案 1 :(得分:1)

You can remove the pause event before you open the gallery and reattach the event on the gallery's callback.

document.removeEventListener("pause", myFunction);

答案 2 :(得分:1)

我不确定您是否正在使用Ionic 1或2,但对于这两个版本,您都有生命周期事件。

以下是Ionic 2的内容(向下滚动到&#34; Lifecycle events&#34;) https://ionicframework.com/docs/api/navigation/NavController/

以下是Ionic 1的产品 https://forum.ionicframework.com/t/order-of-lifecycle-events/28251/2

相关问题