Ionic pause and resume events triggering multiple times in Android

#1

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!!

#2

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

#3

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

#4

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

#5
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?

#6
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.

1 Like
#7

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.

#8

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

#9

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