Ionic如何处理双击返回键就能退出APP

2015-08-04 廖小正

Ionic如何处理双击返回键就能退出APP的功能?

.run(function($ionicPlatform, $rootScope, $location, $ionicHistory, $cordovaToast, $timeout) {
	$ionicPlatform.registerBackButtonAction(function (e) {
		e.preventDefault();
		// Is there a page to go back to?
		if ($location.path() == '/main' ) {
			if ($rootScope.backButtonPressedOnceToExit) {
				ionic.Platform.exitApp();
			} else {
				$rootScope.backButtonPressedOnceToExit = true;
				$cordovaToast.showShortTop('Press again to exit APP.');
				setTimeout(function () {
					$rootScope.backButtonPressedOnceToExit = false;
				}, 2000);
			}
		} else if ($ionicHistory.backView()) {
			$ionicHistory.goBack();
		} else {
			// This is the last page
			$rootScope.backButtonPressedOnceToExit = true;
			$cordovaToast.showShortTop('Press again to exit APP.');
			setTimeout(function () {
				$rootScope.backButtonPressedOnceToExit = false;
			}, 2000);
		}
		return false;
	}, 101);
})