Method initialize(disable) when leave page in Ionic


#1

I have several pages in test app.
If each pages name is as page1, page2, page3 …
If I leave from page1 to page2, then how to disable all methods from previous page?
Problem is using on NFC API on each pages, but always working previous methods even if I leave there.
How to disable or initialize it? :frowning:

Page1

  ionViewDidEnter() {
    this.nfc.enabled().then(() => {
      this.addListenNFC();
    }).catch(() => {
      alert("NFC not support.");
    });
  }

ionViewWillLeave(){
this.nfc.unshare(); //even if I wrote this, it working when I leave this page…
//How to implement code to disable below methods?
}

  // NFC event register
  addListenNFC() {
    this.myListener = this.nfc.addTagDiscoveredListener((data) => {
      return data;
    }, (err) => {
      this.popupService.showCommonPopup(err);
      alert('NFC not detected!');
      }).subscribe((event) => {
        this.myListener.unsubscribe(); //remove NFC Event
      return this.onNfcLogin(event);
    });
  }
  onNfcLogin(data) {
    if (data && data.tag && data.tag.id) {
      this.scanned = true;
        this.tagId = this.nfc.bytesToHexString(data.tag.id);
        this.userService.nfclogin(this.tagId).subscribe(data => {
            this.granted = true;
          this.popupService.showCommonPopup(data);
          this.resetScanData();
          this.loading.dismiss();
          this.events.publish('user:login');
          this.navCtrl.pop();
        },
          error => {
            this.denied = true;
            alert('This card is not registered');
            this.resetScanData();
          this.loading.dismiss();
          this.events.publish('user:login');
          this.navCtrl.pop();
          });
    }
}

#2

I solved with “unsubscribe()” when triggered ionViewDidLeave().

  ionViewDidLeave() {
    this.myListener.unsubscribe();
   }