【发布时间】:2019-12-03 19:08:54
【问题描述】:
有什么方法可以知道我们的 ionic 应用程序正在关闭或被杀死?
【问题讨论】:
有什么方法可以知道我们的 ionic 应用程序正在关闭或被杀死?
【问题讨论】:
您可以订阅Ionic platform service 的pause 事件。文档这样描述暂停事件:
本机平台放置应用程序时会发出暂停事件 进入后台,通常当用户切换到不同的 应用。
David M. 就如何实现这个here 给出了一个很好的答案:
import { Component } from '@angular/core';
import { Subscription } from 'rxjs';
import { Platform } from 'ionic-angular';
@Component({...})
export class AppPage {
private onPauseSubscription: Subscription;
constructor(platform: Platform) {
this.onPauseSubscription = platform.pause.subscribe(() => {
// do something when the app is put in the background
});
}
ngOnDestroy() {
// always unsubscribe your subscriptions to prevent leaks
this.onPauseSubscription.unsubscribe();
}
}
首先,您必须将Platform 服务注入您的页面/组件,然后您才能订阅pause 事件。我只是稍微编辑了 Davids 代码示例,所以请给他一些功劳。
Ionics Platform 服务只是 Cordovas 事件的包装器。因此,如果您更感兴趣,请查看 events 或 pause event 上的 Cordova 文档。
【讨论】: