【问题标题】:Change back button (android) behaviour when Popup is active, ionic当 Popup 处于活动状态时更改后退按钮 (android) 行为,离子
【发布时间】:2016-11-15 20:02:47
【问题描述】:

我正在努力实现这一目标:

场景 1

    • 用户按回。
    • 出现弹出窗口询问用户是否要退出 *
    • 用户按回。
    • 应用退出*

场景 2

  1. 用户按回。

    • 出现弹出窗口询问用户是否要退出 *
    • 用户按下取消
    • 弹出窗口关闭
    • 用户按回。
    • 出现弹出窗口询问用户是否要退出 *
    • 用户按下返回
    • 应用退出。

我尝试使用registerBackButtonActiononHardwareBackButtoncombination 但我无法让退出弹出窗口显示第二次(第二种情况),它只是退出。

这是我现在的代码:

 var exitPopupControl = function(event) {
            //if I press back again, just go out
            $ionicPlatform.onHardwareBackButton(function(event2){
                navigator.app.exitApp();
            });

          if($state.current.name === "app.startseite"){
                $ionicPopup.confirm({
                    title: 'Exit',
                    template: 'Do you want to exit? <br /><small>Press Back again to exit.</small>'
                }).then(function(res) {
                    if(res) {
                        navigator.app.exitApp();
                        $rootScope.exitPopupShowed = false;

                    } else {
                        console.log('I choose not to left the app');
                        $ionicPlatform.registerBackButtonAction(exitPopupControl, 100);
                    }
                });
            }
            else {
                window.history.back();
            }
        };

    $ionicPlatform.registerBackButtonAction(exitPopupControl, 100);

【问题讨论】:

标签: angularjs ionic-framework


【解决方案1】:

我使用以下 wa 进行了处理 - 只需更改确认弹出代码,

var exitPopupControl = function(event) {
            //if I press back again, just go out
            $ionicPlatform.onHardwareBackButton(function(event2){
                navigator.app.exitApp();
            });

          if($state.current.name === "app.startseite"){
                $ionicPopup.confirm({
                    title: 'Exit',
                    template: 'Do you want to exit? <br /><small>Press Back again to exit.</small>'
                }).then(function(res) {
                    if(res) {

                        $rootScope.exitPopupShowed = false;
                        navigator.app.exitApp();

                    } else {
                        return;
                    }
                });
            }
            else {
                window.history.back();
            }
        };

    $ionicPlatform.registerBackButtonAction(exitPopupControl, 100);

或者您可以尝试以下方法:

document.addEventListener("deviceready", function() {
    document.addEventListener("backbutton", function(e) {
       $ionicPopup.confirm({
            title: 'Exit',
            template: 'Do you want to exit? <br /><small>Press Back again to exit.</small>'
        }).then(function(res) {
            if(res) {

                $rootScope.exitPopupShowed = false;
                navigator.app.exitApp();

            } else {
                return;
            }
        });
    }, false);
}, false);

或者你可以试试吐司,我更喜欢用这个,

var countTimerForCloseApp = false;
$ionicPlatform.registerBackButtonAction(function(e) {
 e.preventDefault();
 function showConfirm() {
  if (countTimerForCloseApp) {
   ionic.Platform.exitApp();
  } else {
   countTimerForCloseApp = true;
   showToastMsg($cordovaToast, 'Press again to exit.');
   $timeout(function() {
    countTimerForCloseApp = false;
   }, 2000);
  }

 };

 // Is there a page to go back to?
 if ($ionicHistory.backView()) {
  // Go back in history
  $ionicHistory.backView().go();
 } else {
  // This is the last page: Show confirmation popup
  showConfirm();
 }

 return false;
}, 101);

谢谢

【讨论】:

  • 您在弹出窗口中添加了return,然后是function->then->else部分,它仍然会在场景2中退出应用程序。
  • 尝试我的 OR 部分答案,没有任何功能,javascript 在上述功能中自行处理后退按钮调用,
  • 和关于 return 仍然存在于场景 2 中,因为我认为它调用你的代码 $ionicPlatform.onHardwareBackButton(function(event2){ navigator.app.exitApp(); });
  • 是的,这就是我不知道如何控制的。
  • 直接尝试我的 OR 部分,无需任何函数调用,请在 cotroller 中写入,但不要在任何$scope.function 中写入
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-04-13
  • 1970-01-01
  • 1970-01-01
  • 2020-01-04
  • 2013-08-19
  • 2015-03-24
  • 1970-01-01
相关资源
最近更新 更多