TheUnreal
TheUnreal

Reputation: 24492

Ionic 2 execute method when user leaves the app

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?

Upvotes: 4

Views: 4836

Answers (2)

TheUnreal
TheUnreal

Reputation: 24492

  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');
            });
        });
    

Upvotes: 13

avilac
avilac

Reputation: 802

I think what are you looking for can be found here:

http://cordova.apache.org/docs/en/6.x/cordova/events/events.html#endcallbutton

Specifically this event:

function onEndCallKeyDown() {
    // Handle the end call button
}

In order to make the event work you have to declare this listener first:

document.addEventListener("endcallbutton", onEndCallKeyDown, false);

onPause Event:

document.addEventListener("pause", onPause, false);

function onPause() {
    // Handle the pause event
}

onResume event

document.addEventListener("resume", onResume, false);

function onResume() {
    // Handle the resume event
}

Hope it helps!

Upvotes: 0

Related Questions