gpt4 book ai didi

android - 覆盖android硬件后退按钮,使其在一页退出并在下一页返回

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:11:09 27 4
gpt4 key购买 nike

我正在使用以下 Controller ,以便在 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);


});

现在的问题是,当我转到以下 View 时,它仍然用作退出按钮,但我希​​望它只是任何其他不是仪表板的 View 中的后退按钮,所以我在下面尝试了这个 Controller :

angular

.module('az-app')
.controller('DirMedicoController', function ($scope, $state, $ionicPlatform) {

$ionicPlatform.registerBackButtonAction(function () {

navigator.app.backHistory();

}, 100);

});

所以现在它返回了功能,但是当我在 dashboard 时,如果我从过去的 Controller 按下返回,它会覆盖它的功能,现在它不会退出回来。

更新

感谢 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');
}

});

最佳答案

在您的仪表板 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);

并添加 $location 和 $ionicHistory 作为依赖

.controller('DashboardController', function ($scope, $state, $ionicPlatform, $location, $ionicHistory) {

从其他 Controller 中删除 registerBackButtonAction

关于android - 覆盖android硬件后退按钮,使其在一页退出并在下一页返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32340317/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com