Question

Using Cordova-2.7.0 for Android I have the following JS script.

While testing the back-button functionality, I faced with a weird behavior.

At the very first run of the application, when I press the back-button 'backbutton' event is triggered and 'onBackButton' function is called.

When I exit the application and run the application again, 'onPauseButton' function is called instead of 'onBackButton' function after pressing the back-button.

After a detailed study, I realized that 'navigator.app.exitApp();' (which is cordova function) does not destroy the Android application totally.

If I delete the application from the recent apps list and run it again, 'backbutton' event is triggered and 'onBackButton' function is called when I press the back-button.

So, I want to catch 'backbutton' event in each run of the application.

What do you suggest me to do?

Thanks, 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();
    }
} 
Was it helpful?

Solution

I do not know if it will solve your issue. But according to your code, it is possible that you try to call some Cordova methods whereas Cordova has not been loaded yet.

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();
    }
}

Please see phonegap doc link about events: http://docs.phonegap.com/en/2.7.0/cordova_events_events.md.html#Events

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top