覆盖 android 硬件后退按钮,使其在一页退出并在下一页返回
Override android hardware back button, make it exit in one page and make it back in the next pages
我正在使用以下控制器,以便在 dashboard
页面 android 后退按钮退出应用程序,但在其余页面上它会返回。因为在 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
时如果我从过去的控制器按回它会覆盖它的功能而不是退出它回去了。
更新
感谢 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');
}
});
在您的仪表板控制器中执行此操作
$ionicPlatform.registerBackButtonAction(function() {
//var path = $location.path()
if ($location.path() === "/dashboard" || $location.path() === "dashboard") {
navigator.app.exitApp();
}
else {
$ionicHistory.goBack();
//navigator.app.goBack();
}
}, 100);
并添加 $location 和 $ionicHistory 作为依赖项
.controller('DashboardController', function ($scope, $state, $ionicPlatform, $location, $ionicHistory) {
从其他控制器中删除 registerBackButtonAction
。
我正在使用以下控制器,以便在 dashboard
页面 android 后退按钮退出应用程序,但在其余页面上它会返回。因为在 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
时如果我从过去的控制器按回它会覆盖它的功能而不是退出它回去了。
更新
感谢 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');
}
});
在您的仪表板控制器中执行此操作
$ionicPlatform.registerBackButtonAction(function() {
//var path = $location.path()
if ($location.path() === "/dashboard" || $location.path() === "dashboard") {
navigator.app.exitApp();
}
else {
$ionicHistory.goBack();
//navigator.app.goBack();
}
}, 100);
并添加 $location 和 $ionicHistory 作为依赖项
.controller('DashboardController', function ($scope, $state, $ionicPlatform, $location, $ionicHistory) {
从其他控制器中删除 registerBackButtonAction
。