2017-12-08 9 views
1

私のホームページ内にregisterbackbuttonactionをオーバーライドしました。unregistering私のバックボタンアクションionViewDidLaave私のホームページから別のページにプッシュするとうまくいきます。モデルへの移動時またはサイドメニューから別のページに移動するときにデバイスのバックボタンの登録を解除する方法

問題:

私は、サイドメニューの項目については、私のapp.component.tsファイル内this.nav.push("pagename")を書かれています。このため、私はデバイスの戻るボタンを押し過ぎた後、デバイスの戻るボタンをクリックして戻ることができません。

私はアプリ全体からオーバーリディングバックボタン機能を削除すると、私は期待している出力を得ています。

また、modelページを開くと、ライフサイクルイベントが発生していない場合にも発生します。

は、ここで私は、それがバックボタンの登録を解除されたが、モデルとsidemenu部分は、それが動作していないプッシュしないとき

tsは上記のコードは正常に動作します

initializeBackButtonCustomHandler(){ 

    let self = this 
    this.platform.ready().then(() => { 

    self.unregisterBackButtonAction = self.platform.registerBackButtonAction(() => { 
     if (this.menuCtrl.getOpen() != null) { 
     this.menuCtrl.close(); 
     return 
     }else if(this.modal && this.modal.index === 0) { 
     console.log("invoking modal value") 
     /* closes modal */ 
     // this.modal.dismiss(); 
     this.viewCtrl.dismiss(); 
     return 

     } 
     this.toast = this.toastCtrl.create({ 
     message: "Press back again to exit the app", 
     position: 'bottom', 
     duration: 1500 
     }); 
     this.toast.present(); 
     this.clickCount = this.clickCount + 1; 
     this.toast.onDidDismiss(() => { 
      this.clickCount = 0; 
     }) 

     if(this.clickCount == 2){ 
     this.clickCount = 0; 
     this.toast.dismiss(); 
      this.platform.exitApp(); 
     } 
    }, 10) 
    }); 
    } 


    ionViewDidEnter(){ 
    this.initializeBackButtonCustomHandler(); 

} 

    ionViewDidLeave() { 
    this.unregisterBackButtonAction && this.unregisterBackButtonAction(); 

} 

ファイル私のコードです。

答えて

0

戻るボタンの登録を解除する代わりに、前のページに戻ることができます。前のページに戻ると、戻る状態がないときにアプリケーションを終了できます。私は次のコードがこれを達成するのに役立つと思います。

platform.registerBackButtonAction(()=>{ 
      let nav = this.app.getActiveNav(); 
      if(nav.canGoBack()){ 
       nav.pop(); 
      }else{ 
       let ismodalopened=this.app._appRoot._modalPortal.getActive(); 
       if(ismodalopened){ 
        app.navPop(); 
       }else{ 

        let alertController = this.alertCtrl.create({ 
         title: 'Confirm', 
         message: 'Are you sure you want to exit?', 
         buttons: [{ 
         text: "Exit", 
         handler:() => { platform.exitApp(); } 
         }, { 
         text: "Cancel", 
         role: 'cancel' 
         }] 
        }) 
        alertController.present(); 
       }   
      } 
     }); 
+0

私が登録を解除する必要がある理由は、すべてのページをハンドリングする必要がなく、あるページでregisterbackbuttonactionを実行した場合でも、残りのページに影響を及ぼしているためです。 –

0

app.componentは特別なコンポーネントです。事実上、それはあなたのアプリケーション全体であるため、イオンライフサイクルイベントは利用できません。イオンページだけがそれらの出来事を持っています。

また、バックボタンリスナーの優先度を10から0に下げようとしています。私のチームがapp.componentで実装した例ですが、最近、あなたはアプリケーションを閉じる前にモーダルポップアップを追加しました。

/** 
* Method will listen for native backButton on Android 
* Back: 
* If not at nav root will perform nav.pop() same as ionic menu back button. 
* Login: 
* Because of the behavior of backButton on mobile we need to force 
* the user back to the Azure logon page if they are not logged in . 
* Close App: 
* If at a root page and android will ask user if want to close app. Then react to yes/no. 
*/ 
public setupMobileBackButtonListener(): void { 
    if (PlatformUtils.isMobile()) { 
     this.platform.registerBackButtonAction(() => { 
      let user = this.memoryStoreProvider.loginMemoryData().data; 
      if (cmp.isUndefinedOrNull(user)) { 
       this.platform.exitApp(); 
      } 

      if (cmp.isDefinedAndNotNull(this.nav) && this.nav.length() === 1) { 
       this.modalProvider.openYesNoActionModal(CLOSE_APPLICATION_QUESTION_MESSAGE).then(result => { 
        if (result === YES_NO_MODAL_YES_SELECTED) { 
         this.platform.exitApp(); // no back pages in queue exit app 
        } 
       }); 
      } else { 
       this.nav.pop(); // backPage 
      } 
     }, 0); 
    } 
} 

リスナーを0にすることはありません。これの欠点は、イオンバックメニューボタンのように動作させる余分なロジックです。

関連する問題