简体   繁体   English

Cordova backbutton事件无法正常工作

[英]Cordova backbutton event not working

I am currently using Apache Cordova version 5.1.1 to build native apps for iOS and Android. 我目前正在使用Apache Cordova 5.1.1版为iOS和Android构建本机应用程序。 Everything seems to be working fine, except the backbutton event inside of Android. 除了Android内部的backbutton事件外,一切似乎都运行正常。

I would like to use the back button to navigate through the app's screens (the app is built using Framework7 ), but for some reason the backbutton event never triggers. 我想使用后退按钮通过应用程序的屏幕导航(应用程序是使用内置Framework7 ),但由于某些原因, backbutton的事件从未触发。 Here's a review of the (simplified) code that I am using: 以下是我正在使用的(简化)代码的评论:

if( window.cordova )
{
    document.addEventListener("deviceready", function() { MRC.init(); }, false);
}

var MRC = 
{
    init: function()
    {
        if( window.cordova )
        {
            cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
            cordova.plugins.Keyboard.disableScroll(true);

            // Prevent the back button:
            document.addEventListener("backbutton", function(e) {

                e.preventDefault();
                MRC.view.router.back();

                return false;

            }, false);
        }
    }f
}

Everything inside of the init() function works, but the backbutton event listener never triggers, and when a user presses their hardware back button on an Android device, the app closes, rather than going to the previous view. 在内心的所有init()函数的工作,但backbutton事件侦听器永远不会触发,并且当用户按下他们的硬件后退按钮在Android设备上,应用程序关闭,而不是去到一个视图。

According to Cordova's documentation: 根据Cordova的文件:

This is an event that fires when the user presses the back button. 这是用户按下后退按钮时触发的事件。

 document.addEventListener("backbutton", yourCallbackFunction, false); 

Details 细节

If you need to override the default back button behaviour you can register an event listener for the 'backbutton' event. 如果需要覆盖默认的后退按钮行为,可以为“后退”事件注册事件侦听器。 It is no longer necessary to call any other method to over ride the back button behaviour. 不再需要调用任何其他方法来覆盖后退按钮行为。 Now, you only need to register an event listener for 'backbutton'. 现在,您只需要为'backbutton'注册一个事件监听器。

Typically, you will want to attach an event listener with document.addEventListener once you receive the Cordova 'deviceready' event. 通常,一旦收到Cordova的“deviceready”事件,您将需要使用document.addEventListener附加事件侦听器。

Supported Platforms 支持的平台

  • Android Android的
  • BlackBerry WebWorks (OS 5.0 and higher) BlackBerry WebWorks(OS 5.0及更高版本)
  • Windows Phone 7 and 8 Windows Phone 7和8

Has anyone encountered a similar problem here, or is there a workaround that I have missed? 有没有人在这里遇到类似的问题,还是有一个我错过的解决方法?

Now, I do not know how well this works on Windows Phone and Webworks, but another easy way to catch the back button is to properly use the HTML5 history.pushState API. 现在,我不知道它在Windows Phone和Webworks上的效果如何,但另一种捕获后退按钮的简单方法是正确使用HTML5 history.pushState API。 Simply speaking this means that if you load up your app in the browser that the URL is changing with each navigation through the app and if properly implemented this even means that things like 'resume from memory' will work perfectly (something that's often not the case for phonegap apps) and thus is better than binding the backbutton event. 简单来说,这意味着如果您在浏览器中加载您的应用程序,URL正在通过应用程序的每次导航进行更改,如果正确实现,这甚至意味着“从内存中恢复”这样的操作将完美地工作(通常情况并非如此)为PhoneGap的应用程序),因此比结合更好backbutton事件。 It seems that framework7 supports this using the pushState: true option on the Framework7() init. 似乎framework7使用Framework7() init上的pushState: true选项支持这一点。 To be honest I am somewhat worried by the fact that in the description they say 说实话,我有点担心他们说的描述

If you develop web app (not PhoneGap or Home Screen web app) it is useful to enable 如果您开发Web应用程序(不是PhoneGap或主屏幕Web应用程序),则启用它很有用

Which goes directly against my own experience and makes me worry that the Framework7 implementation has some rough edges, but maybe this is just a problem on IE or something? 这直接违背了我自己的经验,让我担心Framework7实现有一些粗糙的边缘,但也许这只是IE上的问题或其他什么? (The only proper implementation of a history supported PhoneGap app I build was for Android and ran on top of Chrome (as is the default for the newest PhoneGap apps I think)) (我构建的历史支持的PhoneGap应用程序的唯一正确实现是针对Android并在Chrome上运行(我认为最新的PhoneGap应用程序的默认设置))

//var APP_PLUGIN_NAME = Number(cordova.platformVersion.split('.')[0]) >= 4 ? 'CoreAndroid' : 'App';
var APP_PLUGIN_NAME ='App'

Please try this above the code cordova.js. 请在代码cordova.js上面试试这个。 Your issue will resolve.Even i am also faced same problem when i upgrade cordova version from 3.6.6 to 5.1.1 您的问题将解决。即使我将cordova版本从3.6.6升级到5.1.1,我也遇到了同样的问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM