【问题标题】:Ionic 3 pause and resume events triggering multiple times in AndroidIonic 3暂停和恢复事件在Android中多次触发
【发布时间】:2018-01-19 04:28:32
【问题描述】:

我在 ionic 3 暂停和恢复事件中遇到奇怪的问题,每当我暂停应用程序时,事件都会调用多次(一次 2,一次 3 等)。 这是代码

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

我尝试在 ionViewDidLoad、Constructor、ionViewWillEnter 中放置相同的代码,但仍然面临同样的问题。请任何人帮助我解决这个问题。应用程序恢复后我会调用一项服务,但现在它会调用多次。谢谢!!

【问题讨论】:

  • @Suraj Rao.. 祝你好运.. 我在 userdashboard 页面中调用这些事件.. 不在 app.component.js 中.. 这可能是问题吗??
  • 该页面是否延迟加载?导航到什么时候?我认为这可能取决于这些.. 但不确定为什么会在构造函数中多次调用它
  • 这些事件只调用了多次。构造函数和生命周期方法没有调用

标签: android angular ionic3 ionic-native


【解决方案1】:

它会调用多次,因为每次您暂停或恢复将订阅平台的应用程序时。 您需要按以下方式取消订阅平台

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

我希望它会起作用。

【讨论】:

  • 感谢您的回答,我会检查并通知您
【解决方案2】:

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

}

强文本

即使您有多个标签,它也可以工作 你可以对暂停事件做同样的事情

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-06-19
    • 2017-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-10
    • 1970-01-01
    相关资源
    最近更新 更多