我使用以下控制器,以便在 dashboard
页面中安卓后退按钮退出应用,但在其余页面上它会返回。在 dashboard
页面之前,我有一个教程,我只向用户提供一次,然后我必须覆盖android后退按钮,以便在 dashboard
如果按下则会退出,它可以正常使用此代码:
angular
.module('az-app')
.controller('DashboardController', function ($scope, $state, $ionicPlatform) {
/**
* While user on dashboard.html we don't want Android back button to return
* to tutorial views so we override it so that in case that back button is pressed
* to exit app which is in accordance with android lifecycle.
*
*/
$ionicPlatform.registerBackButtonAction(function () {
if($state.is('/dashboard') || $state.is('dashboard')){
navigator.app.exitApp();
}
}, 100);
});
现在的问题是,当我转到以下视图时,它仍然可以作为退出按钮,但我希望它只是一个不是仪表板的任何其他视图中的后退按钮,所以我试过这个以下控制器:
angular
.module('az-app')
.controller('DirMedicoController', function ($scope, $state, $ionicPlatform) {
$ionicPlatform.registerBackButtonAction(function () {
navigator.app.backHistory();
}, 100);
});
所以现在它会返回功能,但是当它在 dashboard
时再次按下来自过去的控制器它会覆盖它的功能,而现在它不会退出它。 / p>
感谢mudasser ajaz的回答,我终于可以回答:
angular
.module('az-app')
.controller('DashboardController', function ($scope, $state, $ionicPlatform, $location, $ionicHistory) {
/**
* While user on dashboard.html we don't want Android back button to return
* to tutorial views so we override it so that in case that back button is pressed
* to exit app which is in accordance with android lifecycle.
*
* Else if not on dashboard just work as normal back button
*
*/
$ionicPlatform.registerBackButtonAction(function() {
if ($location.path() === "/dashboard") {
navigator.app.exitApp();
}
else {
$ionicHistory.goBack();
}
}, 100);
$scope.backToPolicy = function () {
$state.go('intro');
}
$scope.showDirMedico = function () {
$state.go('dirmedico');
}
});
答案 0 :(得分:20)
Do this in your dashboard controller
$ionicPlatform.registerBackButtonAction(function() {
//var path = $location.path()
if ($location.path() === "/dashboard" || $location.path() === "dashboard") {
navigator.app.exitApp();
}
else {
$ionicHistory.goBack();
//navigator.app.goBack();
}
}, 100);
And add $location and $ionicHistory as dependency
.controller('DashboardController', function ($scope, $state, $ionicPlatform, $location, $ionicHistory) {
Remove registerBackButtonAction
from other controller.