Cordova Windows Phone 8 Exit Application

If you have a Phonegap/cordova app that goes from page1.html to page2.html then follows a link back to page1.html the standard backbutton behavior wont exit the app.

Page1 >> Page 2 >> Page 1 — Windows Phone will take you to page 2 instead of exiting the app. It’s expected behavior but it’s kinda poorly documented..

Anyway I searched around for ages but didn’t find a fix that worked for me..

Diff friendly folks see the commit that includes this fix

TLDR of how I fixed this is to use a JS value to track which page I’m on..

IE in page1.html you could have..

var currentPage = "index";

Then in your app.deviceready function include..

if(currentPage == "index"){
  history.go(-(history.length-9999));
  document.addEventListener("backbutton", handleBack, true);
}else{
  document.addEventListener("backbutton", handleBack, false);
}
function handleBack(){
  // handle other logic here such as handling the back events from page2 to page1..
}

The real magic here is history.go(-(history.length-9999)) which basically tells the history stack to reset. Also the true statement on addEventListener allows the original registered event to fire (Native back button)..

Anyway give it a try and let me know if it works for you.

Your system does not meet the requirements to create android projects: undefined

Cordova platform add android errors with

Your system does not meet the requirements to create android projects: undefined

Did you remember to

chown -R YOURUSERNAMEHERE /usr/local/lib/node_modules/cordova

You can check with..

ls -lsh /usr/local/lib/node_modules/cordova

You should see your username, not root..

If so..
Let’s do a quick update of Cordova:

cordova -v
# review the version you are at 
npm update cordova
cordova -v
# review the new version

Let’s make sure android works and the path is set to the android binary:

android

This brings up the Android SDK Manager.

Test an emulator by hitting up:
Tools > Manage AVDS, creating an image then hit Start. If your Android emulator works then continue, else something is up with your ADT/SDK deployment

Open up a CLI, type:

export | grep PATH

Make sure your Android SDK platform-tools and tools folders are available in the PATH variable.

Going okay? Let’s update the Android SDK

android update sdk

Now try visit your cordova app folder and try

cordova platform add android

Still no luck?
Try using the master2 branch of the cordova-cli repo *Warning, this is dangerous.

cd ~
git://github.com/apache/cordova-cli.git
cd ~/cordova-cli
git checkout master2
sudo npm install -g
exit
cordova -v

The above should output a new cordova version #

Post a comment! :)