【问题标题】:Ionic prevent hardware back previous page离子防止硬件返回上一页
【发布时间】:2015-10-13 04:33:12
【问题描述】:

我正在尝试使用以下方法通过硬件后退按钮阻止返回上一页:

$ionicPlatform.onHardwareBackButton(function () {
    if($state.$current.name=="authentication") {
        $ionicPopup.confirm({
            title: 'System warning',
            template: 'Are you sure you want to exit?'
        }).then(function(res){
           if( res ){
              navigator.app.exitApp();
           }
        })
    }
});

但它不起作用。当警报弹出框出现时,它仍然返回到上一页。

我怎样才能阻止它?

【问题讨论】:

  • 我想我对this question的回答可以帮助到你。
  • @AndreKreienbring 感谢您的评论。但我想要的是防止它返回上一页,也不要在某些页面上退出应用程序。
  • 这应该完全可以通过我发布的代码来实现,因为您可以决定如何对 backButtonAction 做出反应。通过删除此行 navigator.app.backHistory(); 您可以防止应用程序返回历史记录。当然,你也可以对任何你喜欢的 $state 这样做。
  • @AndreKreienbring 不管有没有navigator.app.backHistory();,问题还是会回到上一页。
  • 你可以试试这个 .run(function($ionicPlatform, $ionicPopup) { // 禁用主页上的 BACK 按钮 $ionicPlatform.registerBackButtonAction(function(event) { if (true) { // 你的检查这里 $ionicPopup.confirm({ title: '系统警告', template: '你确定要退出吗?' }).then(function(res) { if (res) { ionic.Platform.exitApp(); } }) } }, 100); });

标签: cordova ionic-framework


【解决方案1】:

你可以试试这个,

.run(function($ionicPlatform, $ionicPopup) {
  $ionicPlatform.registerBackButtonAction(function(event) {
    if (true && $state.$current.name=="authentication") { // your check here
      $ionicPopup.confirm({
        title: 'System warning',
        template: 'are you sure you want to exit?'
      }).then(function(res) {
        if (res) {
          ionic.Platform.exitApp();
        }
      })
    }
  }, 100);
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-10-04
    • 1970-01-01
    • 2018-05-14
    • 2015-12-30
    • 1970-01-01
    • 2020-01-03
    • 2022-10-25
    • 1970-01-01
    相关资源
    最近更新 更多