2
votes

Is there any way to handle back button (device backbutton) as default functionality to move back page? I need to implement the same functionality on back button goes to previous page. If there is no previous page (first page) it exit the application. Is this possible in PhoneGap?
I also need to pop page before going to push another page is this posible in jQuery?

1
You need to use navigate event.Omar
can you please give me example..here there not much description..?user2484319

1 Answers

1
votes

Checking window.location.length would be the easiest way to determine if you're on the first page, but this isn't available in Phonegap.

But since you're using JQM, you can either use the navigate event as Omar suggests or could manually count the number of pages shown and the number of pages gone back (same thing) and use this to determine if the first page is being shown and whether to exit the app. Something like this would work:

var pageHistoryCount = 0;
var goingBack = false;

$(document).bind("pageshow", function(e, data) {
    if (goingBack) {
        goingBack = false;
    } else {
        pageHistoryCount++;
        console.log("Showing page #"+pageHistoryCount);
    }
});

function exitApp() {
    console.log("Exiting app");
    navigator.app.exitApp();
}

function onPressBack(e) {
    e.preventDefault();
    if(pageHistoryCount > 0) pageHistoryCount--;
    if (pageHistoryCount == 0) {

        navigator.notification.confirm("Are you sure you want to quit?", function(result){
            if(result == 2){
                exitApp();
            }else{
                pageHistoryCount++;
            }
        }, 'Quit My App', 'Cancel,Ok');
    } else {
        goingBack = true;
        console.log("Going back to page #"+pageHistoryCount);
        window.history.back();
    }
}

function deviceready() {
    $(document).bind('backbutton', onPressBack);
}
$(document).bind('deviceready', deviceready);

As for the second part of your question:

Secondly i need to pop page before going to push another page is this posible in jquery ?

It's not clear what you're asking here. Do you mean you want to show some kind of popup content like a dialog between every page change? Please clarify then I can help you :-)