Is there any way to know that our ionic app is getting closed or killed?
You can subscribe to the pause
event of the Ionic platform service. The docs describe the pause event like this:
The pause event emits when the native platform puts the application into the background, typically when the user switches to a different application.
David M. gave a good answer on how to implement this 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();
}
}
First of all, you'll have to inject the Platform
service into your page/component and then you can subscribe to the pause
event. I only edited Davids code sample slightly, so please give him some credit.
Ionics Platform service is merely a wrapper around Cordovas events. So if you're further interested, check out the Cordova docs on events or the pause event.