2013-05-14 56 views
1

Android的Cordova-2.7.0我有以下JS脚本。Phonegap(科尔多瓦) - 后退按钮触发暂停事件,而不是Android中的后退事件

在测试后退按钮功能时,我遇到了一个奇怪的行为。

在应用程序的第一个跑,当我按下后退按钮“后退按钮”事件被触发并“onBackButton”函数被调用。

当我退出该应用程序并再次运行该应用程序,“onPauseButton”函数被调用按后退按钮之后,而不是“onBackButton”功能。

经过详细的研究,我意识到'navigator.app.exitApp();'(这是cordova函数)不会完全破坏Android应用程序。

如果我删除从最近使用的应用列表中的应用程序并再次运行它,“后退按钮”事件被触发,当我按下后退按钮“onBackButton”函数被调用。

因此,我想在应用程序的每次运行中捕获'backbutton'事件。

你建议我做什么?

感谢,V.H.

initialize: function() { 
    document.addEventListener('deviceready', this.onDeviceReady, false); 
    document.addEventListener('backbutton', this.onBackButton, true); 
    document.addEventListener('pause', this.onPauseButton, true); 
}, 

onDeviceReady: function() { 
    console.log("onDeviceReady called"); 
}, 

onPauseButton: function() { 
    console.log("onPauseButton called"); 
}, 

onBackButton: function() {   
    console.log("onBackButton called"); 
    console.log("current view: "+GUIManager.currentView); 

    if(GUIManager.VIEW_LOCALE == GUIManager.currentView){ 
     GUIManager.showMatchListScreen(); 

    } else if(GUIManager.VIEW_MATCHLIST == GUIManager.currentView){ 
     navigator.app.exitApp(); 
    } 
} 

回答

1

我不知道这是否会解决您的问题。但根据您的代码,您可能会尝试调用某些Cordova方法,而Cordova尚未加载。

initialize: function() { 
    document.addEventListener('deviceready', this.onDeviceReady, false); 
}, 

onDeviceReady: function() { 
    console.log("onDeviceReady called"); 
    document.addEventListener('backbutton', this.onBackButton, true); 
    document.addEventListener('pause', this.onPauseButton, true); 
}, 

onPauseButton: function() { 
    console.log("onPauseButton called"); 
}, 

onBackButton: function() {   
    console.log("onBackButton called"); 
    console.log("current view: "+GUIManager.currentView); 

    if(GUIManager.VIEW_LOCALE == GUIManager.currentView){ 
     GUIManager.showMatchListScreen(); 

    } else if(GUIManager.VIEW_MATCHLIST == GUIManager.currentView){ 
     navigator.app.exitApp(); 
    } 
} 

请参阅有关事件的PhoneGap文档链接:http://docs.phonegap.com/en/2.7.0/cordova_events_events.md.html#Events

+0

这能帮助非常多。但有趣的是,设备不需要准备好进行'document.addEventListener'调用。它只将给定的监听器注册到文档对象内的给定事件类型。 – vaha 2013-05-14 08:30:50