ionic android下双击返回退出应用

.run(function($ionicPlatform, $location, $rootScope, $timeout, $cordovaToast, $ionicHistory) {

    //please install  cordova and cordovaToast

    $ionicPlatform.registerBackButtonAction((function(e) {
      /*
       Your back button action will override each of the above actions
       whose priority is less than the priority you provide. For example,
       an action assigned a priority of 101 will override the ‘return to
       previous view’ action, but not any of the other actions.
       */
      //ionic.Platform.exitApp();

      /**
       * 判断是否为需要双击退出页面
       * @returns {boolean}
       */
      function isRootPage(){

        //判断处于哪个页面时双击退出
        var arr = $location.path().split("/");

        if(arr.length <= 3){
          return true;
        }
        return false;

      }

      if(isRootPage()){

        if ($rootScope.backButtonPressedOnceToExit) {
          ionic.Platform.exitApp();
        } else {
          $rootScope.backButtonPressedOnceToExit = true;
          $cordovaToast.showShortTop('再次点击退出应用');
          setTimeout(function () {
            $rootScope.backButtonPressedOnceToExit = false;
          }, 2000);
        }

      }else{
        $ionicHistory.goBack();
      }

      //e.preventDefault();

    }), 101);

  })

 

posted @ 2015-10-30 16:02  未来造梦师  阅读(289)  评论(0编辑  收藏  举报