2017-05-05 135 views
1

在我的Ionic 2应用程序中,硬件后退按钮(android,windows)应该像他在默认情况下那样工作,但有一个例外:如果存在没有什么可以回来的。Ionic 2 - 防止退出应用程序硬件后退按钮

我知道我能做到这一点是这样的:

platform.ready().then(() => { 
    platform.registerBackButtonAction(() => { 
     // Default action with the exception here 
    },); 
}); 

但我怎么都必须完全现在就做,使之为默认值,但与这一个例外的工作?在ionic 1 docs of that function中有不同情况的优先级。但我认为在离子2中发生了变化!因为在ionic 2 docs没有这些优先事项。我试图将优先级设置为99,因为那么一切都应该作为默认值工作。但是现在sidemenu不能再被关闭了,这就是为什么我认为离子1的优先级已经改变了,因为在离子文件中,sidemenu的优先级是150,这就是为什么我的功能应该被忽略。有人可以帮助我吗?

+0

与此相关的一个:https://stackoverflow.com/questions/40539573/ionic-2-cancel-hard-back-button-override-to-close-app-on-back-button-when -use – ApriOri

回答

9
constructor(public nav: NavController, private platform: Platform, public menu: MenuController) { 
     platform.ready().then(() => { 
      // Okay, so the platform is ready and our plugins are available. 
      // Here you can do any higher level native things you might need 

      platform.registerBackButtonAction(() => { 
      if(this.menu.isOpen()){ 
       this.menu.close() 
      } 
      else if(this.nav.canGoBack()){ 
       this.nav.pop(); 
      }else{ 
       //don't do anything 
      } 
      }); 
     }); 

     } 
+0

没有那不是默认的操作...旁边的菜单例如不会关闭你的代码。 – Nono

+0

对于这种情况,我编辑了代码。请检查它是否适合您的情况 – nabin