Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 398
Mallikarjun Hampannavar Avatar asked Sep 14 '25 09:09

Mallikarjun Hampannavar


2 Answers

It is calling multiple times becuase every time you pause or resume the application it will subscribe to platform. you need to unsubscribe to platform as following

private sub1$:any;
private sub2$:any;

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

ionViewWillUnload() {
    this.sub1$.unsubscribe();
    this.sub2$.unsubscribe();
  }

I hope it will work.

like image 141
Borad Akash Avatar answered Sep 16 '25 00:09

Borad Akash


import { Platform } from '@ionic/angular';
import { Component } from '@angular/core';
import { Subscription } from 'rxjs';


@Component({
  selector: 'app-tab1',
  templateUrl: 'tab1.page.html',
  styleUrls: ['tab1.page.scss']
})
export class Tab1Page {


  constructor(private platform:Platform) {}
  resumeListener:Subscription=new Subscription();
ionViewWillEnter() {
  this.resumeListener=this.platform.resume.subscribe(()=>{
    console.log("resume")

  })
}

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

}

it will work even if you have multiple tab same thing you can do with pause event

like image 43
harun_me Avatar answered Sep 15 '25 23:09

harun_me