1

I'm trying to port existing phonegap application (iOS/Android) to WP7. Everything works fine, except code for handling "back" button. Currently there is handler for back button, which does reloads page if logic asks for it, or closes application if user is in "first"page.

Code:

if (doLogic) {
  // some update logic
} else  {
  // try to close app
  device.exitApp();
}

Unfortunately, althought, update logic works, I'm unable to close app.

PiRX
  • 3,515
  • 1
  • 19
  • 18

4 Answers4

2

You cannot forcibly exit the application on wp7. It's in the Wp7 design guidelines.

Let the user press back, and don't cancel it if you want to let him exit.

TDaver
  • 7,164
  • 5
  • 47
  • 94
  • I'm not sure that it's possible with phonegap event handlers. At least I haven't found a way to not suppress default handler for back button. – PiRX Apr 12 '12 at 13:05
  • can't you "return true" as I'd do in jQuery/HTML? – TDaver Apr 12 '12 at 13:12
  • can you point me to sample, to see what you are talking about? Thanks! – PiRX Apr 18 '12 at 07:17
  • @PiRX: in jQuery: http://stackoverflow.com/questions/4379403/jquery-event-handlers-return-values – TDaver Apr 18 '12 at 11:22
0

Like mentioned by @TDaver, Windows Phone applications are not meant to be closed. Trying to close them is against's the dev guidelines and the application might be rejected.

just let the default navigation stack mechanism take care of it.

Hermit Dave
  • 3,036
  • 1
  • 13
  • 13
-1

To exit the application, wherever page you are in:

 if (NavigationService.CanGoBack)
 {
     while (NavigationService.RemoveBackEntry() != null)
     {
         NavigationService.RemoveBackEntry();
     }
 }
Dante
  • 3,833
  • 4
  • 38
  • 55
-1

Seems that with current PhoneGap implementation it is impossible to add back button handler and still allow application to close with back button.

Solved this with little hacking into PhoneGap, by changing page_BackKeyPress method in CordovaView.xaml.cs

Changed if(OverrideBackButton) to

    string cancel = "false";
    if (OverrideBackButton)
    {
        try
        {
            cancel = CordovaBrowser.InvokeScript("shouldCancelBackButton") as string;
            Console.WriteLine("CancelBackButton response: " + cancel);
        }
        catch (Exception ex)
        {
            Console.WriteLine("Exception while checking for backbutton cancel into cordova view: " + ex.Message);
        } 

        try
        {
            e.Cancel = cancel == "true"; 
            CordovaBrowser.InvokeScript("CordovaCommandResult", new string[] { "backbutton" });
        }
        catch (Exception ex)
        {
            Console.WriteLine("Exception while invoking backbutton into cordova view: " + ex.Message);
        }
    }

Now I can add shouldCancelBackButton JS function in page and based on state suppress or execute default back button handler behaviour.

Probably it's not the best solution, but unfortunately I have no time for getting more familiar with PhoneGap internals.

PiRX
  • 3,515
  • 1
  • 19
  • 18