0

Possible Duplicate:
Android - How To Override the “Back” button so it doesn't Finish() my Activity?

I am developing an app for android/ios using kendo UI and phonegap. In the android version I have a navigation bar that only has a home button which takes the user to home page. when the user is in home page, If I click back, it will go to the previous page. How can I stop the back button on android from navigating back to previous page and does nothing on home page ?

thank you

Community
  • 1
  • 1
Rob Schneider
  • 679
  • 4
  • 13
  • 27
  • The suggested "Possible Duplicate" answer is not adequate since this question is asked in terms of of an app created using Phonegap/Kendo UI (i.e. javascript) and not one programmed Java. – zkent Mar 17 '14 at 09:14

1 Answers1

1

You may try to add an event listener to your back button to prevent its usual behavior:

document.addEventListener("backbutton", function(e){

    // Get pathname / url
    var pathname = window.location.pathname;

    // If the pathname / url corresponds to your homepage's one:
    if(pathname==homepage_pathname){
        e.preventDefault();            
    }
    else {
        navigator.app.backHistory();
    }
}, false);

where homepage_pathname is the pathname/url of your homepage.

Hope this helps mate.

Littm
  • 4,923
  • 4
  • 30
  • 38
  • What does window.location.pathname return ? Can I refer to my homepage by its id ? as in the if condition say if(pathname == #home) ? – Rob Schneider Sep 19 '12 at 10:43
  • It should return the entire url, so it's something like this: `http://www.blabla.com/yourpage.html` . So, I guess for your case it would be something like this: `http://www.blabla.com/yourpage.html#home` . Since I do not know exactly the structure of your code, I based my solution on something general: the url. – Littm Sep 19 '12 at 10:50
  • okay I just had a search and it return the url after the domain. but how can I refer to my home page url ? is there a way that I could do something similar by referring to the Id of the div containing home page ? – Rob Schneider Sep 19 '12 at 10:50
  • Could you show your code? So that maybe we could find a better and easier way I guess :) – Littm Sep 19 '12 at 10:51
  • well its a huge project and I dont know which bits of it I should show, but basically I have created various pages within div tag and all of them apart from home page has this navigation bar that has the home button on it which takes the user back to home. if instead of pathname, I could get the id of the current page and then match it to the id of home page, I think I could use the rest of the code – Rob Schneider Sep 19 '12 at 11:18