简体   繁体   English

无法使用sencha touch 2和cordova处理android的后退按钮

[英]Unable to handle the back button of android using sencha touch 2 and cordova

I am developing a sencha touch 2 application and I am packaging it using cordova for android. 我正在开发sencha touch 2应用程序,并且正在使用cordova for android打包它。 I want to handle the back button of android through cordova. 我想通过cordova处理android的后退按钮。 I have written the below code in app.js file of my project, after adding all the views in the viewport. 在视口中添加所有视图之后,我在项目的app.js文件中编写了以下代码。

if (Ext.os.is('Android')) {
          document.addEventListener("backbutton", Ext.bind(onBackKeyDown, this), false);

          function onBackKeyDown(e) {
              e.preventDefault();

              if (Ext.Viewport.getActiveItem().xtype == myHomeView.xtype ) {
                  navigator.app.exitApp();
              } else {
                 Ext.Msg.alert("Back button is pressed");
              }
          }
       }

Also, I built it using command line. 另外,我使用命令行构建它。

1. cmd --> cordova create app com.basicapp.app "App"
2. Deleted all the content inside "www" folder and replaced with my project's content.
3. cmd --> cordova platform add android 
4. Installed device plugin from cordova documentation.
5. cmd --> cordova build

But I am unable to handle the backbutton in the device.It just exits the application in all the views. 但是我无法处理设备中的后退按钮,它只是在所有视图中退出应用程序。 Is the device ready event not getting fired? 设备就绪事件不会触发吗? Can anyone please explain me what am I doing wrong here. 谁能解释一下我在做什么错。

I was able to get this working by doing the following: 通过执行以下操作,我可以使此工作正常进行:

  1. Added this to my index.html 将此添加到我的index.html

    <script type="text/javascript"> document.addEventListener("backbutton", MyApp.app.nativeBack(), false); </script>

  2. Defined this function within my application 在我的应用程序中定义了此功能

    nativeBack: function() { console.log("back button was pushed..."); // navigationView.pop(), etc... }

I'm packaging using PG Build, but it should work the same. 我使用PG Build打包,但是应该可以正常工作。

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

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