Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ionic 2 execute method when user leaves the app

Tags:

ionic2

I'm trying to execute a method when the user leaves the app. I tried everything:

ionViewWillUnload() {
  console.log("Wlill unload");
  this.leaveRoom();
}

onDestroy() {
  console.log("DESTROY");
  this.leaveRoom();
}

ionViewWillLeave() {
  this.leaveRoom();
}

Unfortunetly they are not executed when user closes the app or when the user refresh the page.

Any idea?

like image 817
TheUnreal Avatar asked Dec 11 '22 13:12

TheUnreal


1 Answers

  1. Import Platform:

    import { Platform } from 'ionic-angular';

  2. Add Platform to the constuctor:

    constructor(public navCtrl: NavController, platform: Platform)

  3. subscribe to the platform pause and resume:

         platform.ready().then(() => {
            this.platform.pause.subscribe(() => {
                console.log('[INFO] App paused');
            });
    
            this.platform.resume.subscribe(() => {
                console.log('[INFO] App resumed');
            });
        });
    
like image 176
TheUnreal Avatar answered May 28 '23 14:05

TheUnreal