Phonegap(Cordova) - 后退按钮在Android中触发暂停事件而不是后退按钮事件

时间:2013-05-14 07:01:32

标签: android cordova cordova-2.0.0

使用Cordova-2.7.0 for Android我有以下JS脚本。

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

在应用程序的第一次运行时,当我按下后退按钮'后退'事件被触发并且'onBackButton'函数被调用。

当我退出应用程序并再次运行应用程序时,按下后退按钮后会调用'onPauseButton'函数而不是'onBackButton'函数。

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

如果我从最近的应用列表中删除该应用并再次运行,则会触发'backbutton'事件,当我按下后面时会调用'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 个答案:

答案 0 :(得分: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 doc链接:http://docs.phonegap.com/en/2.7.0/cordova_events_events.md.html#Events

相关问题