离子防止硬件后退按钮退出应用程序 [英] Ionic prevent exit app on hardware back button

查看:148
本文介绍了离子防止硬件后退按钮退出应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ionic来开发混合应用程序。但最近我发现每当我点击硬件后退按钮时,无论在哪个页面,应用程序都会退出。我尝试添加以下代码,它确实显示了警告框,但这并没有阻止应用退出(不会点击警告框)。

I am using ionic to develop hybrid app. But recently I found that whenever I click on the hardware back button, the app will exit no matter on which page. I tried to add the following code, it does shows the alert box but this doesn't stop the app from exit (alert box is not clicked).

$ionicPlatform.onHardwareBackButton(function() {
  alert("click on hardware back button");
 }

如何防止应用退出硬件后退按钮?

How can I prevent the app from exit from hardware back button?

推荐答案

通常情况下,当你在根视图上并且堆栈上没有历史记录时,应用程序就存在。

Normally the apps exists when you're on a root view and there's no history on the stack.

你可以拦截事件registerBackButtonAction 并取消操作。

You can intercept the event registerBackButtonAction and cancel the action.

必须发生注册当你运行你的应用程序时:

The registration of the even must happen when you run your app:

.run(function($ionicPlatform) {
        $ionicPlatform.registerBackButtonAction(function(e) {
            e.preventDefault();
        }, 1000);   
});

这是签名:

registerBackButtonAction(callback, priority, [actionId])

正如您所看到的那样,优先级为第二个参数。根据文档:

As you can see there's a priority as second parameter. According to the documentation:


现有后退按钮挂钩的优先级如下:

返回上一个视图= 100

关闭边菜单= 150

关闭模态= 200

关闭操作表= 300

关闭popup = 400

关闭加载overlay = 500

您的后退按钮操作将覆盖优先级低于您提供的优先级的上述每个操作。例如,分配优先级为101的
操作将覆盖返回
之前视图操作,但不会覆盖任何其他操作。

The priorities for the existing back button hooks are as follows:
Return to previous view = 100
Close side menu = 150
Dismiss modal = 200
Close action sheet = 300
Dismiss popup = 400
Dismiss loading overlay = 500
Your back button action will override each of the above actions whose priority is less than the priority you provide. For example, an action assigned a priority of 101 will override the 'return to previous view' action, but not any of the other actions.

我用1000来覆盖其他每个动作。
即使您想要在取消活动之前检查事件,注册和收听也总是更好。
如果你按照这条路径行驶,在某些时候你的应用程序会因某种导航而变得更加复杂,你将无法退出你的应用程序。

I've use 1000 to override each of the other actions. It's always better to register and listen to this even as you might want to check the event before cancelling the event. If you follow this path and at some point your app become more complex with some sort of navigation, you won't be able to exit your app.

这篇关于离子防止硬件后退按钮退出应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆