Ionic pause and resume events triggering multiple times in Android

I am facing strange issue in ionic 3 pause and resume events, whenever I paused the application, events are calling multiple times(once 2, once 3 etc). Here is the code

this.platform.ready().then(() => {
 this.platform.pause.subscribe(() => {        
     console.log('****UserdashboardPage PAUSED****');
 });  
 this.platform.resume.subscribe(() => {      
     console.log('****UserdashboardPage RESUMED****');
 });
});

I have tried by placing the same code in ionViewDidLoad, Constructor, ionViewWillEnter still facing same issue. Please anybody help me to get resolve this. I’m calling one service once the app is resumed but now its calling multiple times.Thanks!!

1 Like

You need to unsubscribe when you leave the page, or it stays live.

I have tried unsubscribe the event But next time its not subscribing again

Then you did it wrong. If you want help, you could post code or ask a more specific question.

this.platform.ready().then(() => {
 this.platform.pause.subscribe(() => {        
     console.log('****UserdashboardPage PAUSED****');
     //Same logic
     this.platform.resume.unsubscribe();

 });  
 this.platform.resume.subscribe(() => {      
     console.log('****UserdashboardPage RESUMED****');
     // calling service 
     this.platform.resume.unsubscribe();
 });
});

Is it correct way to unsubscribe the event?

import { Subscription} from 'rxjs/Subscription';
resumeListener: Subscription = new Subscription();

ionViewWillEnter() {
  this.resumeListener = this.platform.resume.subscribe(stuff here);
}

ionViewWillLeave() {
  this.resumeListener.unsubscribe();
}

Wrap it in platform.ready, and do something similar with pause.

2 Likes

In this example I presume that you specify resumeListener in the constructor of the class right?

so something like this:


import { Subscription} from 'rxjs/Subscription';

export class MyPage{
  public resumeListener: Subscription = Subscription;

  constructor(public platform: Platform){
    this.resumeListener = new Subscription();
  }

....
}

Is that correct? I am really struggling to get this to function as I desire. I want code to execute when the application has been moved to the background by the user but not by the app, for instance going to a camera to scan a barcode and then returning to the page to process it. In this instance, with the camera, the whole code is re-executed.

No, you don’t need to. Though the syntax is changing, both with Ionic 4 lifecycle hooks, and the rxjs import changed with v6.

If you need to keep listening but only execute certain logic, you can simply set a toggle variable which will be switched on/off by resume and pause handlers

File: app.components.ts
export class MyApp {

public resumed:boolean = false;

constructor(

this.platform.resume.subscribe((res) => {
if(this.resumed == false){

   //your logic goes here
    this.resumed = true;
  }
  
}); 


this.platform.pause.subscribe((paus) => {
this.resumed = false;
//your logic goes here
});

1 Like

Any solution for this issue ?