Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to subscribe Ionic 2 platform.pause EventEmitter?

I'm trying to use the following code to subscribe, but it doesn't work.

import { Platform } from 'ionic-angular';

@Page({
    templateUrl: 'build/pages/test.html',
})

export class Test {

  constructor(private platform: Platform) {
    this.platform.pause.subscribe(() => {
      console.log('paused')
    });
  }
}

I'm using Ionic 2 with TypeScript, Angular 2. As platform.pause is an EventEmitter provided by Ionic 2, I suppose it should be able to be subscribed. However, when I put the application to the background, console.log('pause') is not fired.

Should I add Platform to providers or something like that? Plus, this.platform is not null. this.platform.ready().then(()=>{console.log('ready')}) works perfectly.

like image 951
Zhipeng YANG Avatar asked Jun 01 '16 09:06

Zhipeng YANG


1 Answers

I think you missed platform.ready() as below

constructor( private platform: Platform ) {
    platform.ready().then(() => {    
        this.platform.pause.subscribe(() => {
            console.log('[INFO] App paused');
        });

        this.platform.resume.subscribe(() => {
            console.log('[INFO] App resumed');
        });
    });
}

The above code worked for me. Hope it helps you as well.

like image 160
Roque Avatar answered Nov 09 '22 08:11

Roque