0

I'm practicing with Cordova. on Android, using also jQuery and jQuery Mobile.
I've a slide menù in my Example App. I check if this menù is open when the variable "global_isOpen". This, with "menubutton", works perfectly:

document.addEventListener("menubutton", function(e){
  if(global_isOpen) {
    closeMenu();
  }
  else{
    openMenu();
  }
}, false);

But I've a problem with "backbutton". This is the code:

document.addEventListener("backbutton", function(e){
  if(global_isOpen) {
    closeMenu(); //is ok
  }
  else{
    navigator.app.backHistory(); //this is the problem
  }
}, false);

the navigator.app.backHistory(); goes to the previous page, but if I'm already on the first page, should come out from the app, exactly like the normal back button of android.
How can I check if I am already on the first page?
I don't have to check if I'm on the home page, because I could go back on the menu. I try to explain better with a scheme.

Example: openApp ---> homePage -> page1 -> page2 -> homePage -> page2 With "backbutton", I should do: page2 -> homePage -> page2 -> page1 -> homePage ---> closeApp

AtanuCSE
  • 8,832
  • 14
  • 74
  • 112
Simone Sessa
  • 795
  • 1
  • 12
  • 35

1 Answers1

0

You can the detect the current page name,and code according to it

var activePage = $.mobile.activePage.attr("id");
if(activePage=="yourHomePageName")
     navigator.app.exitApp();

Here is an example of the page structure and use jsFiddle

AtanuCSE
  • 8,832
  • 14
  • 74
  • 112
  • No.. "I don't have to check if I'm on the home page, because I could go back on the menu." Look the scheme.. With your code, when I'm in home page, and press back, I close the app. – Simone Sessa Sep 19 '14 at 13:05
  • @simone_s1994 ok I misunderstood. What's happening currently when you're on firstpage and this code `navigator.app.backHistory();` executes – AtanuCSE Sep 19 '14 at 18:42
  • Nothing. It doesn't exit.. :/ – Simone Sessa Sep 19 '14 at 19:52
  • I think you'll need to maintain a history array. Whenever you are on a new page you can insert the new pageid in stack and with back button you can delete the top of it. When the array index gets to zero then you can put exit logic. May be you can take the help of `$.mobile.urlHistory.stack[x]` Have a look in this thread http://stackoverflow.com/questions/10884232/how-to-clear-or-change-the-navigation-history-of-jquery-mobile – AtanuCSE Sep 21 '14 at 04:28