ionic项目双击退出没效果
发布于 1年前 作者 xiehua0627 1098 次浏览 来自 问答

从网上找到的双击退出例子,引入了$cordovaToast,$cordovaKeyboard,但实际效果是无论从哪个界面点击返回都是一次就退出了,也没报错。按理说代码应该是没问题的啊 ,请大神指点 下面是一部分代码

angular.module('starter’, ['ionic’, 'starter.controllers’, ‘starter.services’,’ngCordova’])

.run(['$ionicPlatform’,’$rootScope’,’$location’,’$timeout’,’$ionicHistory’,’$cordovaToast’,’$cordovaKeyboard’,function($ionicPlatform, $rootScope, $location, $timeout, $ionicHistory,$cordovaToast,$cordovaKeyboard) { $ionicPlatform.ready(function() { if (window.cordova && window.cordova.plugins && window.cordova.plugins.Keyboard) { cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true); cordova.plugins.Keyboard.disableScroll(true); } if (window.StatusBar) { StatusBar.styleLightContent(); } });

//物理返回按钮控制&双击退出应用
$ionicPlatform.registerBackButtonAction(function (e) {
    //判断处于哪个页面时双击退出
    if ($location.path() == '/tab/meetingRoomInfo') {
        if ($rootScope.backButtonPressedOnceToExit) {
            ionic.Platform.exitApp();
        } else {
            $rootScope.backButtonPressedOnceToExit = true;
            $cordovaToast.showShortBottom('再按一次退出系统');
            setTimeout(function () {
                $rootScope.backButtonPressedOnceToExit = false;
            }, 2000);
        }
    }else if ($ionicHistory.backView()) {
        if ($cordovaKeyboard.isVisible()) {
            $cordovaKeyboard.close();
        } else {
            $ionicHistory.goBack();
        }
    }
    else {
        $rootScope.backButtonPressedOnceToExit = true;
        $cordovaToast.showShortBottom('再按一次退出系统');
        setTimeout(function () {
            $rootScope.backButtonPressedOnceToExit = false;
        }, 2000);
    }
    e.preventDefault();
    return false;
}, 101);

}])

2 回复

这是我的

       // 双击退出
        $ionicPlatform.registerBackButtonAction(function (e) {
            if ($state.includes('tab.default')) {
                if ($rootScope.backButtonPressedOnceToExit) {
                    ionic.Platform.exitApp();
                } else {
                    $rootScope.backButtonPressedOnceToExit = true;
                    ToastService.showShortTop('再按一次退出应用');
                    setTimeout(function () {
                        $rootScope.backButtonPressedOnceToExit = false;
                    }, 2000);
                }
            }
            else if ($ionicHistory.backView()) {
                $ionicHistory.goBack();
            } else {
                if ($rootScope.backButtonPressedOnceToExit) {
                    ionic.Platform.exitApp();
                } else {
                    $rootScope.backButtonPressedOnceToExit = true;
                    ToastService.showShortTop('再按一次退出应用');
                    setTimeout(function () {
                        $rootScope.backButtonPressedOnceToExit = false;
                    }, 2000);
                }
            }
            e.preventDefault();
            return false;
        }, 101);
回到顶部